1

我看到了这个链接(http://stackoverflow.com/questions/6603868/android-onclicklistener-and-table-layout),它似乎对提问的人有用。话虽如此,这就是我的情况。

我有一个 TableLayout,动态填充了四列数据。该行作为一个整体需要是可点击的,因为没有像上面链接的示例那样的按钮。

单击的行需要传递其第一列(第 0 列)的数据,这只是一个字符串。这是调用以创建新行的函数。

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);
}

所以我最初试图制作一个 tr.setOnClickListener(new View.OnclickListener() {blahblah}); 在这个函数中,就在 table.addView(tr) 行之前。“等等等等”当然是一个 onClick(View v) 函数。我本来只会得到最后一行的数据,不管我点击了哪一行。现在,我正在尝试像上面的链接一样,并在创建所有表行之后从更高的函数产生 onclicks。我可能可以提供更多信息,但我认为人们现在可以从中得到这个想法!谢谢你的帮助!

4

1 回答 1

1

底线:我想通了!

根据我在问题中发布的链接的解决方案,我意识到我需要创建一个 textview 对象才能查看单元格的数据!所以这是我的代码,与上面保持不变的代码有关!

    int rowNumCount = table.getChildCount();
    for(int count = 1; count < rowNumCount; count++) {
        View v = table.getChildAt(count);
        if(v instanceof TableRow) {
            final TableRow clickRow = (TableRow)v;
            int rowCount = clickRow.getChildCount();
            v.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Context context = getTabHost().getContext();
                    TableRow row = (TableRow)v;
                    TextView tv = (TextView)row.getChildAt(0);
                    CharSequence text = "Lot VALUE Selected: " + tv.getText();
                    int duration = Toast.LENGTH_SHORT;
                    Toast.makeText(context, text, duration).show();
                }
            });
        }
    }

就像我说的,我只需要获取第一列数据,因此 row.getChildAt(0); 线!所以我知道可能还没有人有机会回答这个问题,但我希望我的回答能在未来帮助其他人!

问题的基本原理,“为什么不只使用列表视图?” 答:我认为对于我正在制作的内容,表格格式看起来要好得多。

虽然我可能不会对我的代码设置进行重大更改,但我总是乐于听取有助于改进我的代码的更改!我爱这个社区!

于 2012-07-26T15:33:49.973 回答