views:

7328

answers:

4

I would like to implement a ListView, which I can do no problem w/ my cursor. Right now depending on which row you click on it takes you to a new activity based on the information pressed on that row (just like it should, and as expected). I would like to have a button however to delete the row, so a user can press any part of the row to launch the new activity, but if they press the button on that row, it deletes the row (or launches a delete activity/function).

If you can look @ DroidRecord, they have a similar layout as I am looking to achive.

Thanks!

Chris.

+1  A: 

Hi Chris, what is your question? How to add a button to a list row?

Very simple, just as you expect it will be added to the row's layout.

Unfortunately though that will also make the whole row "untouchable". The Google developer I asked said that this is by design (as far as I remember), and that you should use TouchDelegate to cope with this. As there are no samples, not even in the android source, and only very thin documentation that didn't work for me

Anyway, it seems that not many applications use a button in the list row. I only know about mine (newsrob, see Articles List) and the Alarm clock. Maybe you could use a context menu?

Otherwise the ugly solution would be to add to call setOnClickListener() on your row view in the getView method.

Cheers

Mariano Kamp
Could you explain how to set it to the row view? My row is getting drawn as part of fill data function I call that uses SimpleCursorAdapter(this,R.layout.row_detail,c, from, to);What method should I override to set an onclick listener. When I try it on my create method, my app crashes.
Chrispix
Sorry, I am not to familiar with the SimpleCursorAdapter ... I use the BaseAdapter directly, but the SimpleCursorAdapter should work pretty much the same. Looking at the documentation I would suspect that bindView is the method to go for you.You get the view (rowview) passed in there.
Mariano Kamp
continued .... To see why your app crashes (and include it in your problem description) you can use "adb -e logcat". That will show you the running emulator's logfile.
Mariano Kamp
I am familiar with the log cat, but I will certainly give that a shot. Thanks.
Chrispix
+1  A: 

It's not the answer to your question, but the long-click/tab is generally the place to pop up a context menu and put extra actions, like delete. You can read how to do it here: http://stackoverflow.com/questions/433761/how-do-you-implement-context-menu-in-a-listactivity-on-android

J. Pablo Fernández
+1  A: 

As Mariano Kamp said, adding buttons to a row will make it "untouchable", but in my experience, this problem goes away if you set these properties on the buttons:

    android:focusable="false"
    android:focusableInTouchMode="false"

See also http://stackoverflow.com/questions/1821871/android-how-to-fire-onlistitemclick-in-listactivity-with-buttons-in-list

BoD
A: 

Thanks BoD, it works for me

Tsvetan Bogoev