Get id ListView elements

0

I have a ListView where I have the buttons of a View of my application. The problem is that I do not know how I can give them "life" by finding their identifier. My question is: How can I get the id of each of the buttons that make up the list?

Deputy code:

Main.java

public class MainActivity extends AppCompatActivity {

static final String[] TITULOS_BTN =
        new String[] { "IBEX 35", "AENA", "BANCO SABADELL", "BANCO  SANTANDER",
                "CAIXABANK", "GAS NATURAL", "FERROVIAL", "IBERDROLA", "TELEFONICA",
                "AMAZON", "MICROSOFT CORPORATION", "APPLE", "NASDAQ", "ORO", "PETROLEO"};

public ListView lvListaDeBotones;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lvListaDeBotones = (ListView)findViewById(R.id.lvListaDeBotones);
        MyAdapter mAdapter = new MyAdapter(this, TITULOS_BTN);
        lvListaDeBotones.setAdapter(mAdapter);

        lvListaDeBotones.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Aquí puedes capturar en on Click de la vista que se crea en general.
            }
        });

        Button btnIbex = (Button) findViewById(R.id.noticiasButton);

        btnIbex.setOnClickListener(new View.OnClickListener(){
                @Override public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), IbexActivity.class );
                startActivity(intent);
                }
         });
}
}

MyAdapter.java

public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;

public MyAdapter(Context context, String[] values) {
    super(context, R.layout.listview_items, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.listview_items, parent, false);
    Button btnAction = (Button) rowView.findViewById(R.id.btnAction);
    btnAction.setText(values[position]);

    return rowView;
}
}

How would the code be in order to find its id?

    
asked by Eduardo 28.04.2017 в 10:26
source

3 answers

2

Well as I was saying, for this you must use holders and tags for your view, although for your case it is not so necessary because you will only load buttons. Replace your class MyAdapter with this:

public class MyAdapter extends ArrayAdapter<String> implements View.OnClickListener{
        private final Context context;
        private final String[] values;
        private int position;

        public MyAdapter(Context context, String[] values) {
            super(context, R.layout.listview_items, values);
            this.context = context;
            this.values = values;
        }

        @Override
        public View getView(int position, View convertView, @NonNull ViewGroup parent) {

            ViewHolder holder = null;
            View rowView = convertView;

            if (rowView == null || !( rowView.getTag() instanceof ViewHolder)) {

                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                rowView = inflater.inflate(R.layout.listview_items, parent, false);
                holder = new ViewHolder();
                holder.btnAction = (Button) rowView.findViewById(R.id.btnAction);
                holder.btnAction.setOnClickListener(this);
                rowView.setTag(holder);
            }else{
                holder = (ViewHolder) rowView.getTag();
            }

            holder.btnAction.setText(values[position]);
            holder.btnAction.setTag(position);
            return rowView;
        }

        @Override
        public void onClick(View v) {
            int position = (int) v.getTag();
            switch (position){
                case 0:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 1:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 2:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 3:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 4:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 5:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 6:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 7:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 8:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 9:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 10:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 11:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 12:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 13:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
                case 14:
                    Toast.makeText(context, values[position], Toast.LENGTH_LONG).show();
                    break;
            }
        }

        private class ViewHolder {
            private Button btnAction;
        }
    }

Or if you want to save the Switch cases, implement RecyclerView for your button, although it is more code and does the same work. Greetings.

    
answered by 28.04.2017 / 12:22
source
1

It's not very clear what you want, I did not understand everything you put, so I'll base this on: How can I get the id of each of the buttons that make up the list?

You can not use an id as a variable, but you can declare everything in the form of a list with their respective ids and use the index of each object in the list to call them. So look:

Button [] btns =new CustomFontTextView[16];

btns[0] = (Button) findviewById(R.id.miBoton1);
btns[1] = (Button) findviewById(R.id.miBoton2);
btns[2] = (Button) findviewById(R.id.miBoton3);
btns[3] = (Button) findviewById(R.id.miBoton4);
btns[4] = (Button) findviewById(R.id.miBoton5);

etc ...

And so you could work with them by calling them by their number as you wish, from a loop to I think you do as you want, Do you want that according to a text is put on a certain button? Then I would pass everything to a Map

Map<String, Integer> botonesPorNombre = new HashMap<>();

botonesPorNombre.put("IBEX 35",0);
botonesPorNombre.put("AENA",1);

etc ... blah blah blah

and when calling the button it would be

btns[botonesPorNombre.get("IBEX 35");

O

btns[botonesPorNombre.get("AENA");

etc ...

Wherever you are going to apply it remains in you, but you can work with cute name buttons.

    public void botonsito(String nombreBotonsito,String textoCool){
    btns[botonesPorNombre.get(nombreBotonsito)].setText(textoCool);
    }

etc etc etc

It's the best that I can explain to you from the cell phone while I walk, luck; D

    
answered by 28.04.2017 в 11:18
1

For this type of cases with dynamic objects the best thing you can do is use "tags" when using ids you have a slight chance that this id is already assigned in the android package and causes problems. I leave you an example of how I would do it. within your method getView() when defining your button you add the tag depending on the position. btnAction.setTag(position); later to access the buttons.

Button btnAction = (Button) padre.findViewWithTag()//el position que desees.
    
answered by 28.04.2017 в 14:41