You need to add the OnClickListner interface to your action, then add the entire dynamic view to setOnClickListner and finally you can catch the click event for the whole view inside onClick (View view) .
try it
public class MainScreen extends Activity implements OnClickListener {
int i = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout tl = (TableLayout) findViewById(R.id.table);
for (int k = 0; k < i; k++) {
TableRow tr = new TableRow(MainScreen.this);
tr.layout(0, 0, 0, 0);
TextView ids = new TextView(MainScreen.this);
ids.setText(loc_id[k]);
ids.setPadding(30, 15, 30, 15);
TextView loc = new TextView(MainScreen.this);
loc.setText(loc_name[k]);
loc.setPadding(30, 15, 30, 15);
tr.setPadding(0, 1, 0, 0);
tr.addView(ids);
tr.addView(loc);
tr.setId(k);
tr.setOnClickListener(MainScreen.this);
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
}
@Override
public void onClick(View v) {
int clicked_id = v.getId();
String ids = loc_id[clicked_id];
String loc = loc_name[clicked_id];
}
}
source
share