在用户从源 SQLite 表中删除一行后,我正在努力更新我的 ListView。
我已经搜索了几天的解决方案并发现了几个推荐我使用的地方,notifyDataSetChanged()
但我似乎无法弄清楚将它放在哪里以及如何使用它。
我正在尝试使用 BaseAdapter,但在查看 LogCat 后我不确定这样做是否正确。
这是我正在使用的代码:
ArrayList<HashMap<String,String>> results = new ArrayList<HashMap<String,String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openAndQueryFav();
displayResultList();
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> item = (HashMap<String, String>) parent.getAdapter().getItem(position);
String Start = item.get("Start").toString();
String Name = item.get("Name").toString();
try{
DBAdapter dbHelper = new DBAdapter(getBaseContext());
newDB = dbHelper.getWritableDatabase();
Cursor favorite = newDB.rawQuery("SELECT Start FROM " +
tableFav + " WHERE Start='" + Start + "'", null);
favorite.moveToFirst();
if(favorite.getCount() > 0){
newDB.execSQL("DELETE FROM " +
tableFav + " WHERE start='" + Start + "'");
HeaderViewListAdapter adapterwrap = (HeaderViewListAdapter) parent.getAdapter();
BaseAdapter adapter = (BaseAdapter) adapterwrap.getWrappedAdapter();
adapter.notifyDataSetChanged();
}else{
}
}catch(Exception e){
Log.d("Events", "catch, exception." + e.toString());
}finally{
newDB.close();
}
}
});
}
private void displayResultList() {
ListAdapter adapter = new SimpleAdapter(
this,
results,
R.layout.list,
new String[] {"Start","Name","date"},
new int[] {R.id.item_id,R.id.item_name,R.id.item_date}
);
setListAdapter(adapter);
}
我一定是做错了什么可怕的事情!因为当我尝试运行它时,我在 LogCat 中得到了这个:
02-14 00:28:54.777: D/Events(9172): catch, exception.java.lang.ClassCastException: android.widget.SimpleAdapter
如果我尝试放置:
HeaderViewListAdapter adapterwrap = (HeaderViewListAdapter) parent.getAdapter();
BaseAdapter adapter = (BaseAdapter) adapterwrap.getWrappedAdapter();
在 try {} 之外,LogCat 中出现此错误:
02-13 23:04:53.107: E/AndroidRuntime(9066): FATAL EXCEPTION: main
02-13 23:04:53.107: E/AndroidRuntime(9066): java.lang.ClassCastException: android.widget.SimpleAdapter
02-13 23:04:53.107: E/AndroidRuntime(9066): at com.myapplication.Favorite$1.onItemClick(Favoritter.java:74)
02-13 23:04:53.107: E/AndroidRuntime(9066): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
02-13 23:04:53.107: E/AndroidRuntime(9066): at android.widget.ListView.performItemClick(ListView.java:3382)
02-13 23:04:53.107: E/AndroidRuntime(9066): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
02-13 23:04:53.107: E/AndroidRuntime(9066): at android.os.Handler.handleCallback(Handler.java:587)
02-13 23:04:53.107: E/AndroidRuntime(9066): at android.os.Handler.dispatchMessage(Handler.java:92)
02-13 23:04:53.107: E/AndroidRuntime(9066): at android.os.Looper.loop(Looper.java:144)
02-13 23:04:53.107: E/AndroidRuntime(9066): at android.app.ActivityThread.main(ActivityThread.java:4937)
02-13 23:04:53.107: E/AndroidRuntime(9066): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 23:04:53.107: E/AndroidRuntime(9066): at java.lang.reflect.Method.invoke(Method.java:521)
02-13 23:04:53.107: E/AndroidRuntime(9066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-13 23:04:53.107: E/AndroidRuntime(9066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-13 23:04:53.107: E/AndroidRuntime(9066): at dalvik.system.NativeStart.main(Native Method)
有人可以指出我正确的方向,以便在执行任务后更新我的列表视图吗?
谢谢!