Android – How to get the row id of a particular selected row

androidandroid-layoutandroid-tablelayout

I have added two TableRows to TableLayout that is in a for loop. In every first TableRow there are 6 TextViews and 1 ImageView & 1 Toggle Button. I have added an OnClickListener to the first row & Toggle Button. My question is in the onclick method for the First Row, how should I get the row id of that particular selected row?

Can someone help me?

Plz help me…..

I want to get the row id of a particular selected row……

Plz anyone reply me….

Best Answer

Navigate through the View hierarchy in the ImageView OnCLickListener like this:

imageView.setOnClickListener(new OnClickListener() {

     public void onCLick(View v) {
          // v represents the view that was clicked(the ImageView)
          // if the ImageView is added directly to the TableRow then you could simply do
          TableRow tr = (TableRow) v.getParent(); // if the ImageView isn't a direct child of the TableRow then walk through all the parent with getParent().
          // do stuff 
     }

});

Edit :

Your code doesn't help and by looking at what you're doing I would advise you to start with something a little simpler to learn about android. Also, tabLayout.addView(openedRow1, n_D); doesn't add a tag to the openedRow1 View(if this is what you want to do), it just position that View to the specific position in the parent(like 0 to be the first child of the parent).

I would guess that you're trying to get the text from the second TextView( tv_vin_val ?!?!) when it's parent is clicked. If this is all you want to do then try something like this:

 public void onClick(View v) {
        // you set the listener on the TableRow so v is the TableRow that was clicked 
        TableRow lTableRow = ((TableRow) v);                                
        // to get the TextView use getChildAt or findViewById
        TextView lTextView = (TextView)lTableRow.getChildAt(1);
        //get the text from the TextView  
        vinNum = lTextView.getText().toString();
        // the tag
        int theTag = (Integer) v.getTag();          
        Intent intent = new Intent(DeliveryInspectionActivity.this, ExceptionsActivity.class);
        //intent.putExtra("row id value", theTag);
        startActivityForResult(intent, 0);
 }