2012-12-25 40 views
2

在我的代碼,現在,爲了正確刷新列表視圖我不得不重新獲取我的數據庫信息並重新創建SimpleCursorAdapter使用notifyDataSetChanged上SimpleCursorAdapter不起作用

例如,我在列表視圖中有一個按鈕。當單擊此按鈕時,它會從列表視圖項目的數據庫中刪除條目。所以我想要發生的是從列表視圖中刪除項目,而不必重新創建適配器。

我試圖改變我從全球到SimpleCursorAdapterBaseAdapater(因爲它擴展SimpleCursorAdapater,並允許使用的notifyDataSetChanged()功能),但它仍然無法正常工作。

這是我現在使用的代碼(沒有工作):

代碼global聲明和onCreate()

private RoutinesDataSource datasource; 
private SimpleCursorAdapter dataAdapter; 
private boolean isEditing = false; 
private Toast toast_deleted; 
private String[] columns = new String[] { MySQLiteHelper.COLUMN_NAME }; 
private int[] to; 

@SuppressLint("ShowToast") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_routines); 

    toast_deleted = Toast.makeText(this, "", Toast.LENGTH_SHORT); 
    datasource = new RoutinesDataSource(this); 
    datasource.open(); 

    Cursor cursor = datasource.fetchAllRoutines(); 
    to = new int[] { R.id.listitem_routine_name }; 
    dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_routine, cursor, columns, to, 0); 
    setListAdapter(dataAdapter); 
} 

代碼爲ListView物品內部的刪除按鈕:

public void onClick(View view) {   
    ListView l = getListView(); 
    int position = l.getPositionForView(view); 

    Cursor cursor = ((SimpleCursorAdapter)l.getAdapter()).getCursor(); 
    cursor.moveToPosition(position); 
    long id = cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.COLUMN_ID)); 
    String name = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME)); 

    switch (view.getId()) { 

     case R.id.button_routine_delete: 
      toast_deleted.setText(getString(R.string.toast_routine_deleted)); 
      toast_deleted.show(); 
      datasource.deleteRoutine(id); 
      onResume(); 
      break; 
    } 
} 

。注意到我使用onResume()的。

我知道datasource.deleteRoutine(id)作品,因爲當我關閉活動,並重新打開它的列表項消失了。

代碼的onResume(),它正確地顯示了清單,刪除列表視圖項:

@Override 
protected void onResume() { 
    datasource.open(); 
    Cursor cursor = datasource.fetchAllRoutines(); 

    if (isEditing) { 
     to = new int[] { R.id.listitem_routine_edit_name }; 
     dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_routine_edit, cursor, columns, to, 0); 
     setListAdapter(dataAdapter); 
    } 
    else { 
     to = new int[] { R.id.listitem_routine_name }; 
     dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_routine, cursor, columns, to, 0); 
     setListAdapter(dataAdapter); 
    } 

    super.onResume(); 
} 

我只是覺得它不好的做法,我只是想要一個列表項中移除,每次已刪除重新創建適配器來自數據庫。就像我說的我已經嘗試了一個BaseAdapater的notifyDataSetChanged,它根本不起作用。

還注意到isEditing布爾的。如果在顯示刪除按鈕的操作欄中單擊編輯按鈕,則該設置爲true。這很有用,因爲我也有一個編輯按鈕,當點擊時它會啓動一個活動,所以當它們在完成編輯後回來時仍然顯示用戶的按鈕。

所以不管怎麼說,能有人指出我如何刷新列表,而無需重新創建適配器 - 或者是我做了什麼的最好方法是什麼?

+1

我在這裏回答了一個類似的查詢 - http://stackoverflow.com/questions/13953171/update-the-listview-after-inserting-a-new-record-with-simplecursoradapter-requ/13953470#13953470 – mango

+0

謝謝。我會嘗試一下。 – scarhand

+0

非常感謝芒果! – scarhand

回答

5

芒果對他的決議的評論中的網址工作完美。

我只是改變了裏面的代碼onResume()這樣:

datasource.open(); 
    Cursor cursor = datasource.fetchAllRoutines(); 
    dataAdapter.changeCursor(cursor); 

    super.onResume(); 

由於onResume()已經打電話給某人,添加或編輯一個項目後,我想它不會傷害稱之爲按下刪除按鈕時考慮到它不再重新創建適配器,而只是簡單地改變光標。

相關問題