Naina Naina - 24 days ago 11
Android Question

How to identify table row id dynamically

I have to find out table row id. For that I am writing the code

match1[i].setTextSize(12);
match1[i].setPadding(10,0,0,0);
tr.addView(match1[i]);
Log.e("TAG", "ID: "+match1[i].getId());
match1[i].setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {

Toast.makeText(SAXParserActivity.this, ""+tr.getId(),
Toast.LENGTH_SHORT).show();
Log.e("TAG", " clicked ID: "+tr.getId());
}
});
t1.addView(tr, tableRowParams);


where match1 is a text view. All is working fine but the problem is on clicking table row. For each row I am getting -1 id, but before on click the id is coming correctly. Please suggest where is the mistake, and how can I display data randomly after parsing?

Answer

Try this Snippet :

final View row=contact_table.getChildAt(i);
row.setOnClickListener(new OnClickListener(){
   public void onClick(View v){
    row_id=contact_table.indexOfChild(row);
   }
});

where contact_table is your TableLayout object