tags:

views:

95

answers:

1

Hi I want to delete an item from a ListView, to which have attached a GestureListener. I have overridden the method onFling for that GestureListener to return true of false based on my criteria.

But I don't know how to fetch the last selected item in the ListView. Fling does not select an item in ListItem.

I am short on time, Could somebody please help. Thanks Nayn

+1  A: 

You do not "delete an item from a ListView".

You need to delete a row from the underlying Adapter. If this is an ArrayAdapter, call delete() on it. If this is a CursorAdapter, call delete() on the database or content provider, then call requery() on the Cursor.

CommonsWare
How to find which item to delete?
Nayn
I have no idea. You're the person trying to do a delete based on a fling. There is no "last selected item in the ListView" if the user is working with the touchscreen. Presumably, when you initiate your fling, you will need to determine which item the user's finger was on at the time.
CommonsWare
hmm. I guess flinging on the screen from left to right for a specific item would seem as if user is throwing it out. So I really want to have that event registered for each item inside the ListView. I'll figure out some other way and update here.
Nayn
@Nayn: Ah! Now I understand what you're trying to do! Actually, I have some code for that, extracted from the Music app in the open source project. Check out http://github.com/commonsguy/cwac-touchlist for a `TouchListView` that supports drag-and-drop, including dragging an item to the screen edge to remove it from the list. It may not meet your needs exactly, but it will demonstrate the techniques. I am far from an expert on the touch events -- like I said, this is from code the Android team used on the Music app -- but it may give you some ideas.
CommonsWare
Thanks for the pointer Mark.
Nayn
Hi Mark. The page does not exist : github.com/commonsguy/cwac-touchlist.git
Nayn
@Nayn: the link works for me. Note that the link does not have the .git on the end.
CommonsWare
Actually i didn't notice the link to download source. I wanted to import the jar instead of using the raw source code. Do you have it uploaded as jar Or should I create it by myself?
Nayn
@Nayn: You cannot package it as a JAR, as it has things that need to live outside of a JAR (e.g., the attributes). It is already packaged as a parcel -- see http://andparcel.com for more information about parcels.
CommonsWare
Perfect. I did not know parcel way of reusing things in android. Thanks.
Nayn