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?