0

我一直在尝试在选择表格行时触发 onClickListener 事件。

表格行在 xml 中定义的表格布局中动态添加(从数组中获取)(在 xml 中仅定义了一个空表格布局)。

更新:
在循环内移动 OnClickListener (用于单选按钮)有效..但不在“tablerow”上。

谁能指出我并纠正我在这方面哪里出错了?如果有人可以为我正在尝试做的事情发布同上示例,我会更加感激。谢谢..

这是我用于填充表格布局的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_selection);

        TableLayout tl = (TableLayout) findViewById(R.id.maintable);




        TextView TVdate = (TextView) findViewById(R.id.textView1_date2);

        Intent getStorei = getIntent();
        // UsrGPSLock = i.getStringExtra("currUsrLat") + "," +
        // i.getStringExtra("currUsrLon");
        userDate = getStorei.getStringExtra("userDate");
        TVdate.setText(userDate);

        TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        uuid = tManager.getDeviceId();



        dbengine.open();

        storeList = dbengine.FetchStoreList();

        dbengine.close();

        /*final String[] storeCode = new String[storeList.length];
        final String[] storeName = new String[storeList.length];*/

        storeCode = new String[storeList.length];
        storeName = new String[storeList.length];

        for (int splitval = 0; splitval <= (storeList.length - 1); splitval++) {
            StringTokenizer tokens = new StringTokenizer(String.valueOf(storeList[splitval]), "_");

            storeCode[splitval] = tokens.nextToken().trim();
            storeName[splitval] = tokens.nextToken().trim();

        }

        System.out.println(storeList);

        // Get the TableLayout
        /*TableLayout tl = (TableLayout) findViewById(R.id.maintable);*/

        // Go through each item in the array
        for (int current = 0; current <= (storeList.length - 1); current++) {

            // Create a TableRow and give it an ID
            tr = new TableRow(this);

            tr.setId(200 + current);
            tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            tr.setClickable(true);

            RadioButton rb1 = new RadioButton(this);

            rb1.setId(current);
            rb1.setText(storeName[current]);
            rb1.setTextColor(Color.BLACK);
            rb1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            tr.addView(rb1);

            CheckBox cb1 = new CheckBox(this);
            cb1.setId(500 + current);
            // cb1.setText(provinces[current]);
            cb1.setTextColor(Color.BLACK);
            cb1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            tr.addView(cb1);

            /*
             * CheckBox cb2 = new CheckBox(this); cb2.setId(300+current);
             * //cb1.setText(provinces[current]); cb2.setTextColor(Color.BLACK);
             * cb2.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT,
             * LayoutParams.WRAP_CONTENT)); tr.addView(cb2);
             */

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

        tr.setOnClickListener(new View.OnClickListener() {

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

                System.out.println("inside-onClick");

                System.out.println(arg0.getId());
                System.out.println(arg0.getTag().toString());

                /*TableRow lTableRow = ((TableRow) arg0);
                RadioButton chkRB = (RadioButton) lTableRow.getChildAt(1);

                Toast.makeText(getApplicationContext(), lTableRow.toString(), Toast.LENGTH_SHORT).show();
                System.out.println(chkRB.getId());
                System.out.println(arg0.getTag().toString());*/

            }
        });

    }

布局 XML:

<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:layout_centerHorizontal="true"
    android:layout_gravity="top"
    android:visibility="visible"
    tools:context=".StoreSelection" >

    <RelativeLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/my_main_border"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/mainImg1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/astix_new3" />

        <TextView
            android:id="@+id/textView1_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="15dp"
            android:layout_toRightOf="@id/mainImg1"
            android:text="Date: "
            android:textSize="12sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView1_date2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/textView1_date"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="1dp"
            android:layout_toRightOf="@id/textView1_date"
            android:text="09-02-2013 "
            android:textSize="12sp"
            android:textStyle="bold" />

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="345dp"
            android:layout_alignLeft="@+id/mainImg1"
            android:layout_below="@+id/mainImg1"
            android:background="@drawable/my_border" >

            <RelativeLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/relativeLayout1111"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/tableheader" >

                <TextView
                    android:id="@+id/textView1_battstat2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginBottom="10dp"
                    android:text="#"
                    android:textSize="12sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textView1_battstat3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="35dp"
                    android:layout_toRightOf="@id/textView1_battstat2"
                    android:text="Store Name"
                    android:textSize="12sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textView1_battstat4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="77dp"
                    android:layout_toRightOf="@id/textView1_battstat3"
                    android:text="Close"
                    android:textSize="12sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textView1_battstat5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="6dp"
                    android:layout_toRightOf="@id/textView1_battstat4"
                    android:text="Next-Day"
                    android:textSize="12sp"
                    android:textStyle="bold" />
            </RelativeLayout>

            <ScrollView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/scrollView111"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/relativeLayout1111"
                android:layout_gravity="top"
                android:fillViewport="true" >

                <RelativeLayout
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <TableLayout
                        xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/maintable"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:stretchColumns="0,1" >
                    </TableLayout>
                </RelativeLayout>
            </ScrollView>

            <View
                android:id="@+id/view2"
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:layout_below="@id/scrollView111"
                android:layout_marginTop="2dp"
                android:background="@android:color/darker_gray" />
        </RelativeLayout>

        <Button
            android:id="@+id/startQues"
            android:layout_width="wrap_content"
            android:layout_height="15dp"
            android:layout_alignLeft="@+id/textView1_date"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/textView1_date2"
            android:layout_below="@+id/relativeLayout1"
            android:layout_marginBottom="0dp"
            android:layout_marginTop="1dp"
            android:clickable="false"
            android:freezesText="false"
            android:gravity="bottom|center_vertical|center_horizontal"
            android:hint="Start"
            android:linksClickable="false"
            android:text="Start"
            android:textSize="15sp" />
    </RelativeLayout>

</RelativeLayout>
4

2 回答 2

1

现在,您已经设置了OnClickListener唯一的最后一个TableRowTableLayout因为您设置了外部OnClickListener 循环for。移动:

tr.setOnClickListener(new View.OnClickListener() {

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

            System.out.println("inside-onClick");

            System.out.println(arg0.getId());
            System.out.println(arg0.getTag().toString());

            /*TableRow lTableRow = ((TableRow) arg0);
            RadioButton chkRB = (RadioButton) lTableRow.getChildAt(1);

            Toast.makeText(getApplicationContext(), lTableRow.toString(), Toast.LENGTH_SHORT).show();
            System.out.println(chkRB.getId());
            System.out.println(arg0.getTag().toString());*/

        }
    });

循环for

于 2013-03-02T07:09:16.447 回答
0

您只需要在 for 循环中添加 onClick 侦听器,它就可以正常工作!

像这样 :

for (int current = 0; current <= (storeList.length - 1); current++) {

            // Create a TableRow and give it an ID
            tr = new TableRow(this);
            tr.setOnClickListener(new View.OnClickListener() {

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

                System.out.println("inside-onClick");

                System.out.println(arg0.getId());
                System.out.println(arg0.getTag().toString());

                /*TableRow lTableRow = ((TableRow) arg0);
                RadioButton chkRB = (RadioButton) lTableRow.getChildAt(1);

                Toast.makeText(getApplicationContext(), lTableRow.toString(), Toast.LENGTH_SHORT).show();
                System.out.println(chkRB.getId());
                System.out.println(arg0.getTag().toString());*/

            }
        });

}
于 2013-03-02T07:46:33.513 回答