ListView 删除项目

ListView remove item

如何删除ListView中的item?我没有找到 remove 函数。

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);

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

您没有从 ListView 中删除项目。您修改或替换 ListAdapter 以不再拥有该项目。在您的情况下,由于您使用的是 SimpleCursorAdapter,您需要:

  • 从数据库中删除项目(在您的情况下,通过 ContentProvider

  • 获取代表查询结果的新 Cursor

  • 调用changeCursor()(如果您使用的是Loader框架)或swapCursor()替换SimpleCursorAdapter中的Cursor用新的

由于您应该 在主应用程序线程的 ContentResolver 上调用 query(),您可能希望切换到 CursorLoader ,在这种情况下,您可以在 onLoadFinished().

中调用 changeCursor()

首先从数据库中删除数据,然后添加

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