表行中的OnClicklistener

时间:2013-04-04 06:48:51

标签: java android onclick row tableview

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

这是活动的完整代码

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监听器并获取特定行的所有列的数据?

我关注了this教程!

2 个答案:

答案 0 :(得分:1)

您必须将setOnClickListener放在tr_data和所有TextView final

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

答案 1 :(得分:0)

您必须将setOnClickListener设置为动态添加的行,即您必须在For Loop中添加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
}

希望这对你有所帮助。