Question

I have a ListView that contains button on each item. I want that the item click will cause the OnItemClick event of the list to fire, as that event contains more info (id,position) than the OnClick event of the button.

This is an important action on the list, that's why I need the button and don't want a regular item click will cause it to fire (I guess making the button focusable will do the trick).

Is this possible?

Was it helpful?

Solution

try to make your own adapter for the list.In getView() method do your necessory requirements.

setListAdapter(new ArrayAdapter<string>(this, R.layout.list_item, strings) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;

if (null == convertView) {
row = mInflater.inflate(R.layout.list_item, null);
} else {
row = convertView;
}

Button bt1 = (Button) row.findViewById(android.R.id.bt1);
bt1.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v){
       //you will get position and do necessary method 
       }
});

return row;
}
});

OTHER TIPS

Is the row clickable also? Depending on if the row is clickable I would probably do one of the following;

  1. You could just set clickable=false on the button, and let the touch propergate up to the parent, namely the list view row the button is in, and onItemClickListener should then be notified for that row

  2. You could set a tag (View.setTag(..)) on the button that reports the position on the row that the button resides in, and read this tag in an onClickListener

  3. You could follow the answer suggested by freshDroid on this page but that would be inefficient as you are creating a new OnClickListener ever time a view is created / recycled which will cause extra GC work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top