2

我创建了一个包含表格视图的活动。标题和表格行通过活动动态添加到表格视图中。一切正常,但我不明白如何将 onClick 列表添加到除标题之外的所有行,并获取特定行中列的值。

这是活动的完整代码

public class ReportListActivity extends Activity {

    TableLayout report_table;
    TableRow tr_data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_list);

        report_table=(TableLayout) findViewById(R.id.report_table);


        //---------------Table Header-----------------------------------------------
        TableRow tr_head = new TableRow(this);
        tr_head.setId(10);
        tr_head.setBackgroundColor(Color.GRAY);
        tr_head.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT));


        TextView label_sr_no = new TextView(this);
        label_sr_no.setId(20);
        label_sr_no.setText("S.No.");
        label_sr_no.setTextColor(Color.WHITE);
        label_sr_no.setPadding(5,5,5,5);
        tr_head.addView(label_sr_no);// add the column to the table row here

        TextView label_test_name = new TextView(this);
        label_test_name.setId(21);// define id that must be unique
        label_test_name.setText("Test Name"); // set the text for the header 
        label_test_name.setTextColor(Color.WHITE); // set the color
        label_test_name.setPadding(5,5,5,5); // set the padding (if required)
        tr_head.addView(label_test_name); // add the column to the table row here

        TextView label_test_date = new TextView(this);
        label_test_date.setId(21);// define id that must be unique
        label_test_date.setText("Date"); // set the text for the header 
        label_test_date.setTextColor(Color.WHITE); // set the color
        label_test_date.setPadding(5,5,5,5); // set the padding (if required)
        tr_head.addView(label_test_date); // add the column to the table row here



        report_table.addView(tr_head, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

      //---------------Table Header-----------------------------------------------



        //--------------------Table Body---------------------------
        for (int i=1; i<=10; i++)
        {
            tr_data = new TableRow(this);
            tr_data.setId(10);
            tr_data.setBackgroundColor(Color.TRANSPARENT);
            tr_data.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));


            TextView sr_no = new TextView(this);
            sr_no.setId(20);
            sr_no.setText(""+i);
            sr_no.setTextColor(Color.BLACK);
            sr_no.setPadding(5,5,5,5);
            tr_data.addView(sr_no);// add the column to the table row here

            TextView test_name = new TextView(this);
            test_name.setId(21);// define id that must be unique
            test_name.setText("Speed Test 60(min) Demo-ST-01"); // set the text for the header 
            test_name.setTextColor(Color.BLACK); // set the color
            test_name.setPadding(5,5,5,5); // set the padding (if required)
            tr_data.addView(test_name); // add the column to the table row here

            TextView test_date = new TextView(this);
            test_date.setId(21);// define id that must be unique
            test_date.setText("12 Mar 2013"); // set the text for the header 
            test_date.setTextColor(Color.BLACK); // set the color
            test_date.setPadding(5,5,5,5); // set the padding (if required)
            tr_data.addView(test_date); // add the column to the table row here



            report_table.addView(tr_data, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        }

      //--------------------Table Body---------------------------


    }


}

这是布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ReportListActivity" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TableLayout
                    android:shrinkColumns="0,1,2"
                    android:id="@+id/report_table" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="match_parent">
            </TableLayout>

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

我应该如何对除标题行以外的表数据实现 onclick 侦听器并获取特定行的所有列的数据?

我跟着这个教程!

4

2 回答 2

1

你必须穿上setOnClickListenertr_data 和所有 TextViewfinal

final TextView test_name = new TextView(this);
            test_name.setId(21);// define id that must be unique
            test_name.setText("Speed Test 60(min) Demo-ST-01"); // set the text for the header 
            test_name.setTextColor(Color.BLACK); // set the color
            test_name.setPadding(5,5,5,5); // set the padding (if required)
            tr_data.addView(test_name); // add the column to the table row here


 report_table.addView(tr_data, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            tr_data.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    String test_name1= test_name.getText().toString();
                    Toast.makeText(getApplicationContext(), test_name1, Toast.LENGTH_LONG).show();
                }
            });
于 2013-04-04T06:56:03.837 回答
0

您必须 setOnClickListener 到动态添加的行,即您必须在 For 循环中添加 SetOnclickListener 动态设置表体

检查您编辑的代码下方

//--------------------Table Body---------------------------
    for (int i=1; i<=10; i++)
    {
        tr_data = new TableRow(this);
        tr_data.setId(10);
        tr_data.setBackgroundColor(Color.TRANSPARENT);
        **tr_data.setOnClickListener(this);**  //set on clickListener to Row here
}

希望这对你有帮助。

于 2014-02-08T10:06:23.790 回答