Reputation: 435
I am having a grid view ,I am passing data which I want to display in grid view to the gridview adaptor class.Now I want to remove the item when an item is long pressed.
I want to delete in onItemLongClickListener .
Upvotes: 0
Views: 2054
Reputation: 104
if mThumbIdsList is the integer array of all the gridview items ids then you can try this code. this may help you.
final ImageAdapter adapter = new ImageAdapter(this);
gridview.setAdapter(adapter);
gridview.onItemLongClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
adapter.mThumbIdsList.remove(position);
adapter.notifyDataSetChanged();
}
});
Upvotes: 1
Reputation: 57336
What you need to do is this:
Create a custom adapter for your GridView. In that adapter, provide a method for removing an item from the list of items it maintains, for example 'void removeItem(int position)`
Call setOnItemLongClickListener
to your grid. In this method, you get the position of the item that the long click occurred on. From this method, call the method to remove the item with the position you just received.
Notify the GridView that the data has changed using notifyDataSetchanged
method. If you want the GridView UI to update immediately, you need to use Handler
for this request to make sure it happens on the UI thread.
Upvotes: 1