Himanshu Aggarwal
Himanshu Aggarwal

Reputation: 1809

Add dynamic data to onClickListener of TextViews in Table Layout

I have made dynamic table rows with two TextViews in each row with a different id (local variable in Java file - see code). I want to open another activity with an image with name say using the same id of the TextView.(which i can send using Intent)

But first, I tried to print the Toast with the same id assigned to the TextView.

// Java Code

    int id = 3;
    for (int i = 1; i <= 3; i++) {

        TableRow row = new TableRow(TableActivity.this);
        // add Layouts to your new row
        TextView txt1 = new TextView(TableActivity.this);
        txt1.setText("" + id);
        txt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "" + id,
                        Toast.LENGTH_SHORT).show();
            }
        });
        id++;

        TextView txt2 = new TextView(TableActivity.this);
        txt2.setText("" + id);
        id++;

        row.addView(txt1);
        row.addView(txt2);

        // add your new row to the TableLayout:
        TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
        table.addView(row);
}

With XML code,

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:stretchColumns="0,1,2,3"
tools:context="com.himanshu.fileimage.TableActivity" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Column1"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Column2"
        android:textColor="#000000" />
</TableRow>

The error comes with the Toast that the id should be a final variable as it is inside an inner class.

So what can be the possible solution for my problem?

Upvotes: 0

Views: 425

Answers (1)

mistwalker
mistwalker

Reputation: 781

That is because you are trying to access a variable local to a method of your outer class inside of a method belonging to another class (View.OnClickListener) that you have defined inline. So that part can be solved by simply declaring your id variable outside of the method as a member to the class of your above code.

However, that will still not solve your problem as you are trying to print the value of id "when" the TextView is clicked but the value of id has already changed (by incrementing it) right after setting the onClickListener at the time of rendering the different views. So you will get a Toast but with an incorrect id value.

To get the correct one you want the TextView that is being touched to know what is its id and to do that you want to set the id as a tag to the TextView like - txt1.setTag(id); right before you change/increment id's value to set it for the next TextView.

Now in the onClick for the View.OnClickListener in the makeToast method pass v.getTag() as the 2nd parameter in place of id and you will get your desired output.

EDIT: BTW, for setting id as the tag on the views it doesn't have to be a class variable, it can be local to the method like you have in your above code. My first paragraph was only to explain why you couldn't access it inside onClick

Upvotes: 3

Related Questions