从包含许多文本视图的动态创建的 table 行获取数据

getting data from dynamically created table row containing many textviews

我动态创建了一个 table 行,其中包含三个 textviews.now 我想在单击 tablerow(tr) 时获取三个 textview 的值。意味着我想要获取 companyTV、valueTV 和 YearTV 的值。 谢谢

    for (int i = 0; i < match.length; i++) {
        /** Create a TableRow dynamically **/
        tr.setBackground(getResources().getDrawable(R.drawable.tv_bg));
        tr = new TableRow(this);
        tr.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.FILL_PARENT));


        /** Creating a TextView to add to the row **/
        companyTV = new TextView(this);
        companyTV.setText(match[i]);
        companyTV.isClickable();
        companyTV.setTextColor(Color.RED);
        companyTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        companyTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        companyTV.setPadding(5, 5, 5, 5);
        tr.addView(companyTV);  // Adding textView to tablerow.

        /** Creating another textview **/
        valueTV = new TextView(this);
        valueTV.setText(kanr[i]);
        valueTV.isClickable();
        valueTV.setTextColor(Color.RED);
        valueTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        valueTV.setPadding(5, 5, 5, 5);
        valueTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tr.addView(valueTV); // Adding textView to tablerow.


        YearTV = new TextView(this);
        YearTV.setText(ort[i]);

        YearTV.setTextColor(Color.RED);
        YearTV.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        YearTV.setPadding(5, 5, 5, 5);
        YearTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tr.addView(YearTV); // Adding textView to tablerow.


        // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));
    }

首先,当您在创建行之前设置 table 行的背景时,您的代码中有一个错误。您应该首先创建新的 table 行,然后设置其背景:

for (int i = 0; i < match.length; i++) {
    /** Create a TableRow dynamically **/
    tr = new TableRow(this);
    tr.setBackground(getResources().getDrawable(R.drawable.tv_bg));

您可以在 OnClickListener

中按索引访问 table 行子视图
        tr.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                TableRow tr = (TableRow)v;
                TextView companyTV, valueTV, yearTV;
                String company, value, year;
                companyTV = (TextView) tr.getChildAt(0);
                valueTV = (TextView) tr.getChildAt(1);
                yearTV = (TextView) tr.getChildAt(2);
                company = companyTV.getText().toString();
                value = valueTV.getText().toString();
                year = yearTV.getText().toString();
            }
        });

如果您想跟踪当前选择了哪 table 行,您可以为 activity

创建全局变量
TableRow selectedTR;

然后在 OnClickListener

中设置
selectedTR = (TableRow)v;

当您使用那个 selectedTR 变量时,请确保检查它是否为空值,以防尚未做出选择。