Functional Clickable TableRows that are created dynamically

给你一囗甜甜゛ 提交于 2019-12-11 07:45:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!