Anil M
Anil M

Reputation: 1307

Is there any solution, where I can write single onClick method for all rows in table in Android?

my requirement is that, I have a table with multiple rows where by selecting single row should display its contents on screen. I figured out that by writing onClick() for every row its achievable, but its not feasible to write multiple onClick()'s for multiple rows. So is there any way to achieve this without writing multiple onClick()'s.

This is my XML for table:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/relativelayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
<ScrollView android:id="@+id/ScrollView01"  
       android:layout_height="100dp" android:layout_width="wrap_content">
    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="wrap_content"
        android:background="#000000"
        android:stretchColumns="*"
        android:layout_height="100dp"
        android:scrollbars="horizontal"
         >

        <TableRow
            android:layout_margin="0.2dip"
            android:background="#000000" >

            <TextView
                android:layout_margin="0.2dip"
                android:background="#FFFFFF"
                android:text="@string/code" />

            <TextView
                android:layout_margin="0.2dip"
                android:background="#FFFFFF"
                android:text="@string/name" />
        </TableRow>

        <TableRow
            android:layout_margin="0.2dip"
            android:background="#000000"
            android:focusable="true"
            android:onClick="showData" >

            <TextView
                android:id="@+id/code1"
                android:layout_margin="0.2dip"
                android:background="#FFFFFF"
                android:text="@string/code1" />

            <TextView
                android:id="@+id/code2"
                android:layout_margin="0.2dip"
                android:background="#FFFFFF"
                android:text="@string/name1" />
        </TableRow>

I have written onClick for second row.

This is onClick() in activity class:

public void showData(View view){
        text = new EditText(this);

        //text = (EditText) findViewById(R.id.displayData);
        TextView tx1 = (TextView) findViewById(R.id.code1);
        TextView tx2 = (TextView) findViewById(R.id.code2);
        TextView tx3 = (TextView) findViewById(R.id.displayData);
        tx3 = new TextView(this);
        String str = tx1.getText().toString();
        String str1 = tx2.getText().toString();
        tx3.setText("CODE : "+str+" "+"NAME :"+str1);
        //text.setText("CODE : "+str+" "+"NAME :"+str1);
        //text.setText("NAME :"+str1);
        setContentView(tx3);
    }

Upvotes: 0

Views: 111

Answers (2)

Pragnani
Pragnani

Reputation: 20155

This may not be the efficient code but you can use this, get your table id

tableLayoutGrid = (TableLayout) findViewById(R.id.yourtableid);
        int count=tableLayoutGrid.getChildCount();
        for(int i=0;i<count;i++)
        {
            TableRow row=(TableRow) tableLayoutGrid.getChildAt(i);
            row.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                       TextView tx1 = (TextView) row.getChildAt(0);
                        TextView tx2 = (TextView) row.getChildAt(1);
                        TextView tx3 = (TextView) findViewById(R.id.displayData);
                        tx3 = new TextView(this);
                        String str = tx1.getText().toString();
                        String str1 = tx2.getText().toString();
                        tx3.setText("CODE : "+str+" "+"NAME :"+str1);

                }
            });
        }

I hope this will help you..

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234807

You can simply use view.findViewById(R.id.code1), etc. (instead of findViewById without the view.) to find the text view specific to each row. That way all rows can use the same android:id values and a single onClick method for all rows. (This is similar to how a ListView works.)

public void showData(View view){
    TextView tx1 = (TextView) view.findViewById(R.id.code1);
    TextView tx2 = (TextView) view.findViewById(R.id.code2);
    . . .
}

If you want to cut down on the sheer size of the xml, you can put the attributes into a style.

Upvotes: 1

Related Questions