milosh
milosh

Reputation: 13

Android remove views from a grid

I was testing the code found at the following Android developer site http://developer.android.com/resources/tutorials/views/hello-gridview.html

Can anyone tell how i can remove item onclick. I have tried removeInLayout which works but the remove image keeps showing up again afterwards. other function removeAt(position) etc are crashing

Any ideas?

Upvotes: 1

Views: 4723

Answers (1)

wjeshak
wjeshak

Reputation: 1064

I don't know how much you've modified hello gridview example, so I will describe the procedure for unmodified example.

First, you have to modify you "data storage" so you can easily remove an item from it, for example create a list from mThumbIds array in the ImageAdapter constructor, store it as a field and remember to modify getView() and getCount() methods to use it.

Next, simply remove an item from the list in onItemClick() method of the listener and then call notifyDataSetChanged() method on the ImageAdapter instance, that you need to store as a final variable

    final ImageAdapter adapter = new ImageAdapter(this);
    gridview.setAdapter(adapter);

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            adapter.mThumbIdsList.remove(position);
            adapter.notifyDataSetChanged();
        }
    });

The trick is that grid view displays items basing on the content of bound data (a list or an array in this example case) , so to remove an item form the grid you have to remove and item from your bound data.

Upvotes: 3

Related Questions