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(...);
.........................................
}
});
I’m imagining that this button is inside the ListView element. Based on this assumption:
create only one OnClickListener. The way you’re doing you’re creating
adapter.getCount()OnClick listener and waste memory like crazy.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.
If you really must have the button, inside your getView you store a reference to the parent, something like that:
then on your click listener you can:
and if that answer works for you (I know it does) accept it as correct like Daniel Martinus pointed out.