问题
I saw this link (http://stackoverflow.com/questions/6603868/android-onclicklistener-and-table-layout) which seems like it works for the person asking the question. That being said, here's my situation.
I have a TableLayout, dynamically filled with four columns of data. The row as a whole needs to be clickable, since there are no buttons like the example linked above.
A clicked row needs to pass its first column's(column 0) data, which is just a string. Here is the function that's called to create a new row.
private void addLotTableRow(CharSequence [] row, int count) {
final TableLayout table = (TableLayout) findViewById(R.id.lotsTableList);
final TableRow tr = (TableRow) getLayoutInflater().inflate(R.layout.lotsrow, null);
TextView tv;
// Fill Cells
tv = (TextView) tr.findViewById(R.id.lotCell1);//Cell 1: Lot Number
tv.setText(row[0]);
tv = (TextView) tr.findViewById(R.id.lotCell2);//Cell 2: Sample
tv.setText(row[1]);
tv = (TextView) tr.findViewById(R.id.lotCell3);//Cell 3: Inspected
tv.setText(row[3]);
tv = (TextView) tr.findViewById(R.id.lotCell4);//Cell 4: Total
tv.setText(row[2]);
table.addView(tr);
}
So I originally tried to make a tr.setOnClickListener(new View.OnclickListener() {blahblah}); in this function, right before the table.addView(tr) line. The "blah blah" was of course an onClick(View v) function. I originally would only get the last row's data, no matter which row I clicked. Now, I'm trying to do like the link above, and produce the onclicks from a higher up function, after all the table rows have been created. I probably can give more info, but I think people can get the idea for now from this! Thanks for any help!
来源:https://stackoverflow.com/questions/11658377/functional-clickable-tablerows-that-are-created-dynamically