odr_m9611
odr_m9611

Reputation: 313

TableRow get tag attribute value

I have 10 category, and show those in TableLayout and every category in a a TableRow. Every category have an ID, I set every category ID in android:tag attribute of each TableRow like this:

<TableLayout>

  <TableRow android:id="@+id/cat1" android:tag="1" onClick="row_click">
    <ImageView />
    <TextView />
    <TextView />
  </TableRow>

  <TableRow android:id="@+id/cat2" android:tag="2" onClick="row_click">
    <ImageView />
    <TextView />
    <TextView />
  </TableRow>
  .
  .
  .

</TableLayout>

And when user click every row, start another activity and send category ID to it.

Now, I have 2 question:

  1. Is this way I choose,corrected? (if no,please suggest a better way)

  2. if yes, how to get value of tag attribute in row_click method?

Upvotes: 0

Views: 1293

Answers (1)

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

yes, do like this

public void row_click(View v){
        System.out.println("HELO : " +v.getTag().toString());
    }

Update

NOTE : android:onClick is for API level 4 onwards, so if you're targeting < 1.6, then you can't use it. So i recommended to use

TableRow tr = (TableRow) findViewById(R.id.tr1);

tr.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        row_click(v);
    }
});

Upvotes: 2

Related Questions