ListView remove item

How to remove item in ListView? I didn't find remove function.

ShopList = (ListView) findViewById( R.id.shopList );
ShopList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
Cursor c = getContentResolver().query(SHOP_LIST_URI, null, null, null, null);
String[] from = { DB.column_name };
int[] to = { android.R.id.text1 };
shopAdapter = new SimpleCursorAdapter(
        this, android.R.layout.simple_list_item_activated_1, c, from, to, 
        SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER );
ShopList.setAdapter(shopAdapter);

Upvotes: 0

Views: 109

Answers (2)

Sabbir Sadik
Sabbir Sadik

Reputation: 36

First remove data from your database and then add

shopAdapter.swapCursor(getContentResolver().query(SHOP_LIST_URI, null, null, null, null));

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006604

How to remove item in ListView? I didn't find remove function.

You don't remove an item from a ListView. You modify or replace the ListAdapter to no longer have the item. In your case, since you are using a SimpleCursorAdapter, you need to:

  • Delete the item from the database (in your case, via the ContentProvider)

  • Get a fresh Cursor representing the result of your query

  • Call changeCursor() (if you are using the Loader framework) or swapCursor() to replace the Cursor in your SimpleCursorAdapter with the new one

Since you should not be calling query() on ContentResolver on the main application thread, you may wish to switch to a CursorLoader, in which case you can call changeCursor() in onLoadFinished().

Upvotes: 3

Related Questions