ListView删除项目

时间:2015-01-11 15:29:29

标签: android listview

如何删除ListView中的项目?我没有找到删除功能。

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

2 个答案:

答案 0 :(得分:3)

  

如何删除ListView中的项目?我没有找到删除功能。

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

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

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

  • 致电changeCursor()(如果您使用Loader框架)或swapCursor(),将Cursor中的SimpleCursorAdapter替换为新query()

由于您应该在主应用程序主题上调用ContentResolver上的CursorLoader,您可能希望切换到changeCursor(),在这种情况下,您可以在onLoadFinished()中拨打{{1}}。

答案 1 :(得分:0)

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

  

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

相关问题