Sindhu Perumalla
Sindhu Perumalla

Reputation: 435

How to delete longpressed item in a grid viw

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

Answers (2)

Abhishek
Abhishek

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

Aleks G
Aleks G

Reputation: 57336

What you need to do is this:

  1. 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)`

  2. 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.

  3. 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

Related Questions