2010-12-15 23 views
1

我有一個listview和CursorAdapter的實現。作爲一個 我的列表項的一部分,我有一個刪除按鈕。當用戶按下 按鈕時,我會顯示一個對話框詢問是否確認,如果用戶按 確定,我從數據庫中刪除該項目。問題是刷新 的列表視圖。我嘗試調用cursor.requery()和 mAdapter.notifyDataSetChanged()(單獨或兩者),但不幫助 。請求清除該列表,並在重新進入該活動後重新出現(不存在丟失的 項目)。 notifyDataSetChanged確實沒有 什麼都沒有(該項目仍在列表中),並且在重新登錄 後再次確認。我已成功 重新加載整個數據庫之後,使這個工作:從對話窗口刷新列表視圖的問題

//in the dialog: 
{ 
DBAdapter db = new DBAdapter(getApplicationContext()); 
db.open(); 

db.deleteTitle(rowid); 

db.close(); 

//cursor.requery(); 
//mAdapter.notifyDataSetChanged(); 

fillData(); 
} 


private void fillData() { 

       try{ 
       db.open(); 
       cursor = db.getAllTitles(); 
       startManagingCursor(cursor); 

       mAdapter = new MyIDsListCursorAdapters(this, R.layout.myidsrow, 
cursor, columns, to); 

       setListAdapter(mAdapter); 
       db.close(); 
      }catch (SQLException e){ 
       showDatabaseErrorDialog(); 
      } 
     } 

但重新加載整個分貝似乎是一個非常昂貴的任務,我 十分肯定必須有一個更好的方式來做到這一點。

我還有另一個問題 - 我的列表視圖項目由 相關佈局定義。然而,佈局似乎忽略了所有'垂直' 屬性,如alignParentBottom或centerVertical。我看到一個 谷歌I/O與羅曼蓋伊和他回答了一個類似的問題 說我們應該通過父ViewGroup其次是虛假的 膨脹函數,但這仍然不能解決我的問題。不知道 這裏發生了什麼事。我解決了這個問題,把我的物品放在 以下,並使用margin/padding來玩,但我不太喜歡 那個解決方案。

+0

你可以在這裏發佈你的適配器實現。 – Vivek 2011-01-04 08:01:00

回答

0

重新查詢已棄用。 在我的實現中,我只是簡單地重新創建了這個似乎可以工作的遊標。這是我的代碼位。就我而言,我使用上下文菜單來標記列表視圖中的項目,但基本方法應該適合您。

private Cursor fetchCursor(){ 
    Intent intent = getIntent(); 
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     return mDbHelper.fetchSpellSearch(query); 
    } else { 
     return mDbHelper.fetchAllSpells(); 
    } 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
    switch (item.getItemId()) { 
     case 0: 
      mDbHelper.updateFavorite(info.id, 1); 
      new bgRequery().execute(); 
      return true; 
     case 1: 
      mDbHelper.updateFavorite(info.id, 0); 
      new bgRequery().execute(); 
      return true; 
     default: 
      return super.onContextItemSelected(item); 
     } 
} 

private class bgRequery extends AsyncTask<Void, Integer, Void> { 
    @Override 
    protected Void doInBackground(Void... voids) { 
     mSpellCursor = fetchCursor(); 
     return null; 
    }