0

在用户从源 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)

有人可以指出我正确的方向,以便在执行任务后更新我的列表视图吗?

谢谢!

4

2 回答 2

0

我找到了解决我的问题的方法。

找到了一个我用来理解问题的工作示例。

主要区别在于,在对所需的数据库进行了更改后,我在光标上使用了“requery()”。

这是我进行所需更改后的代码:

newDB = new DBAdapter(this.getApplicationContext());

        c = newDB
        .getWritableDatabase()
        .rawQuery("SELECT _ID, Start, Name, Time, Fav FROM " +
                tableName + " WHERE Fav='1'" , null);
        ListAdapter adapter = new SimpleCursorAdapter(
                this,
                R.layout.list, c,
                new String[] {"Start","Name","Time"},
                new int[] {R.id.item_id,R.id.item_name,R.id.item_time}
                );
        setListAdapter(adapter);

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {

          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
                String Start = c.getString(c.getColumnIndex("Start"));
                  try{
                      newDB.getWritableDatabase().execSQL("UPDATE " +
                                        tableName + " SET Fav='0' WHERE Start='" + Start + "'");
                      c.requery();

                  }catch(Exception e){

                      Log.d("Events", "catch, exception." + e.toString());
                  }finally{

                  }
                }
          });

Tnx 感谢您@Simon 试图帮助我!

于 2012-02-17T00:29:29.077 回答
0

任何更新 UI 的东西都必须在 UI 线程上运行。尝试这个。

runOnUiThread(new Runnable() {
    public void run() {
        adapter.notifyDataSetChanged();
    }
});

如果直接更改了适配器的底层数据提供程序,例如您在修改数据库时的情况,则必须通知适配器。一种更简洁的方法是扩展适配器类并覆盖插入、添加、删除等。这样,您不需要做任何通知,因为适配器知道它的支持数据已被修改,并将通知任何绑定视图自动地。这种方法的另一个优点是您可以修改数据提供者的底层实现,例如将其移动到 XML 中,而无需在适配器之外更改代码。

于 2012-02-14T00:11:54.810 回答