jER.emY
jER.emY

Reputation: 35

How can remove adapter from Listview, Android

I use SimpleAdapter with custom row layout for Listview

SimpleAdapter adapter = new SimpleAdapter(ListOrder.this, oslist,
                    R.layout.list_v, 
                    new String[] { "name","time","status" }, 
                    new int[] { R.id.tablename, R.id.timeorder, R.id.status});
            list.setAdapter(adapter);

After , i reload data of Listview and i want remove old data. I don't know how i can't remove old data before add new data to Listview.

I used

adapter.remove(adapter.getItem(i));

but have error

The method remove(Object) is undefined for the type SimpleAdapter

Please help me ! Thank you!

Upvotes: 3

Views: 16946

Answers (3)

Margarita Litkevych
Margarita Litkevych

Reputation: 2136

SimpleAdapter does not have remove method so you should extend it to your own adapter and add remove method. E.g.:

private class Adapter extends SimpleAdapter {
    private List<? extends Map<String, ?>> data;

    public Adapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        this.data = data;
        //... place some initializing code here
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //place your code for getView here.
        return super.getView(position, convertView, parent);
    }

    public void remove(int position) {
        if (position >= 0 && data.size() < position && data.get(position) != null) {
            data.remove(position);
            this.notifyDataSetChanged();
        }
    }
}

But if you have array of data that is updated (removed || added) regularly, consider using ArrayAdapter (even without your own extension) that has remove() and other methods (example: http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown ).

Hope it helps :)

Upvotes: 1

Ashish Mohan
Ashish Mohan

Reputation: 118

You can simply set

listview.setAdapter(null);

Upvotes: 10

Suau
Suau

Reputation: 4738

Just modify the underlying List and inform the adapter about it. Like that:

oslist.clear();
adapter.notifyDataSetChanged();

if you only want to remove one or two:

oslist.remove(index);
adapter.notifyDataSetChanged();

Upvotes: 5

Related Questions