我有一个 arraylist 存储数据库中的数据并将其显示在列表视图中,当用户单击一个项目时,数据库中的数据会更新,我想知道如何刷新列表视图中的项目以显示更新的数据。
//Adding item to ArrayList
Cursor cursor2=shoppingListDB.rawQuery("SELECT * FROM ITEMS;", null);
if (cursor2.moveToFirst()){
addArrrayList.clear();
do{
String itemName = cursor2.getString(cursor2.getColumnIndex("ITEM_NAME"));
addArrrayList.add(itemName);
}
while (cursor2.moveToNext());
//====CODE FOR SHOWING DATA AS A SIMPLE LIST ITEM=========================================
ArrayAdapter <String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,addArrrayList);
view_list.setAdapter(adapter);
}
else{
Toast.makeText(getApplicationContext(), "DATA NOT AVAILABLE", 3000).show();
}
cursor2.close();
这是我的 setOnItemClickListener 代码
view_list.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
String click = (addArrrayList.get(arg2).toString());
String[] split = click.split("\\s+");
String fSplit = split[0];
String update = "UPDATE ITEMS SET FOUND = 'YES' WHERE ITEM_NAME =" + "'"+fsplit+"'";
shoppingListDB.execSQL(update);
Toast.makeText(getApplicationContext(), " " + fSplit,3000).show();
}
});
如何使用notifyDataSetChanged
刷新列表视图?