0

I have a ListView items/cell that has a few layers, relative, linearLayout etc. I tried doing v.getParent() inside the onClick of the button but it just went one layer up. How can I get a reference to the root of the current element inside onClick event handler?

Here is my getView()

             Button v = (Button)findViewById(R.id.myButton);

             v.setOnClickListener(new OnClickListener(){
                     onClick(View v) {

                        //Right here I want to find another view inside the cell.
                        View otherView = ???.findViewById(R.id.myOtherViewInListItem); 
                             otherView.setBackgroundColor(...);
                        .........................................
                     } 
              });
user1847544
  • 957
  • 2
  • 9
  • 17

2 Answers2

0

I'm imagining that this button is inside the ListView element. Based on this assumption:

  1. create only one OnClickListener. The way you're doing you're creating adapter.getCount() OnClick listener and waste memory like crazy.

  2. Maybe you want to completely remove the button and use the OnItemClick to track clicks on your whole ListView element, this will pass to you the reference to the whole View.

  3. If you really must have the button, inside your getView you store a reference to the parent, something like that:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // do whatever you're doing to get the view, let's call it v
        Button b = (Button)v.findViewById(/* your button id */ )
        b.setTag(v)
    }
    

then on your click listener you can:

    onClic(View v){
        View parent = (View)b.getTag();
    }

and if that answer works for you (I know it does) accept it as correct like Daniel Martinus pointed out.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • yes, but I think v.getRootView() will go all the way to the top. Then use that view to find it. – user1847544 Dec 19 '12 at 10:29
  • 1
    But if it's an adapter, it's being inflated from XML an all the views have the same ID, so how will you find it? I'll tell you how, with the answer I just posted. – Budius Dec 19 '12 at 10:32
  • on b.setTag(v) you should have posted b.setTag(convertView) etc. Watch that syntax Budius! – user1847544 Dec 19 '12 at 10:46
  • On closer inspection I think you are correct. It is necessary to keep track of specific associated view. – user1847544 Dec 19 '12 at 10:52
  • the code comment clearly states `// do whatever you're doing to get the view, let's call it v`, I'm telling you that I'm calling it `v` and that you have to adapt to whatever you're using. – Budius Dec 19 '12 at 10:53
  • I have a companion question coming up. Stay tuned! – user1847544 Dec 19 '12 at 10:56
  • Here it is! http://stackoverflow.com/questions/13950970/how-to-prevent-modified-listview-item-from-being-reused-or-at-least-not-before – user1847544 Dec 19 '12 at 11:07
0
 Inside onClick(View v)

just call v.getViewParent();  then you will have a view from which you can call the find for   the others.  
user1847544
  • 957
  • 2
  • 9
  • 17