views:

6838

answers:

5

I have a simple ListActivity that uses a custom ListAdapter to generate the views in the list. Normally the ListAdapter would just fill the views with TextViews, but now I want to put a button there as well.

It is my understanding and experience however that putting a focusable view in the list item prevents the firing of onListItemClick() in the ListActivity when the list item is clicked. The button still functions normally within the list item, but when something besides the button is pressed, I want onListItemClick to be triggered.

How can I make this work?

+4  A: 
Ramps
My solution ended up going another way. I solved the problem by setting the descendant Focusability property of the list row layout to block descendants and suddenly it worked. Now I use a simpleCursorAdapter with a custom viewbinder attached to it to do the event setup and other fancy things associated with the buttons.
CodeFusionMobile
A: 

You wrote those lines:

    convertView = mInflater.inflate(R.layout.custom_row, null); 
} 
//take the Button and set listener. It will be invoked when you click the button.
Button btn = (Button) convertView.findViewById(R.id.button); 
btn.setOnClickListener(this); 
//set the text... not important  
...

Who is "this" in that line?

btn.setOnClickListener(this); 

this must be replaces with some listener.

rayman
"this" is an instance of MyAdapter which implements OnClickListener.
Ramps
A: 

if my list has 10 items.. the onclick listener is fired only for the 10th item.. any solution for this ?

Ash
You should start a new question for this, not submit an answer. This isn't a forum style website. There's a lot more information needed in order to properly answer your question.
CodeFusionMobile
A: 

'this' is in the implementation modifier and the onClick() override

Michael SIlveus
A: 

I've had the same problem with ToggleButton. After half a day of banging my head against a wall I finally solved it. It's as simple as making the focusable view un-focusable, using 'android:focusable'. You should also avoid playing with the focusability and clickability (I just made up words) of the list row, just leave them with the default value.

Of course, now that your focusable views in the list row are un-focusable, users using the keyboard might have problems, well, focusing them. It's not likely to be a problem, but just in case you want to write 100% flawless apps, you could use the onItemSelected event to make the elements of the selected row focusable and the elements of the previously selected row un-focusable.

Ridiculous