beerBear
beerBear

Reputation: 979

onClickListener Event Not Firing on a TableLayout and Dynamic n Rows

I have been trying to fire an onClickListener Event on the selection of a table row.

The Table rows are added dynamically (fetched from an array) in a Table layout defined in xml (just an empty table layout is defined in the xml).

Update:
Moving the OnClickListener (for the radio button) inside the loop works..but not on the "tablerow".

Can anyone please point me out and correct me where I am going wrong in this? Will appreciate it more if someone can post ditto samples for what I am trying to do. Thanks..

Here's my code for populating the table layout:

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());*/

            }
        });

    }

Layout 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>

Upvotes: 1

Views: 1047

Answers (2)

Anand
Anand

Reputation: 2885

You need to add just your onClick listener inside for loop and it will work fine !

like this :

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());*/

            }
        });

}

Upvotes: 0

user
user

Reputation: 87064

Right now you've setup the OnClickListener only for the last TableRow from the TableLayout as you set the OnClickListener outside the for loop. Move:

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());*/

        }
    });

inside the for loop.

Upvotes: 1

Related Questions