[英]Android ListView update with SimpleCursorAdapter
嘿,我使用listview來顯示存儲在數據庫中的條目。 我還有一個EditText元素和一個按鈕,它將EditText的內容添加到數據庫中。 要將視圖綁定到數據庫內容,我使用SimpleCursorAdapter和以下populate函數:
private void populate() {
cursor = dbAdapter.getAllItems();
startManagingCursor(cursor);
String[] from = new String[] { DBAdapter.KEY_TASK };
int[] to = new int[] { android.R.id.text1 };
// Now create an array adapter and set it to display using our row
cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to);
list.setAdapter(cursorAdapter);
}
如果我添加了一個新條目,通過單擊我想刷新列表視圖的按鈕,但這只適用於populate函數而不適用於通用適配器函數notifyDataSetChanged();
。 我有錯誤或者這是刷新列表視圖的正確方法嗎?
你有沒有看過這個 ,試過交換游標方法,或者只是簡單地再次調用setAdapter() ?
我有一個類似的問題,我無法讓我的列表更新,我做的只是創建一個refreshListView()方法。 現在你可以從你的onCreate()開始調用它,並在用戶向數據庫添加內容時隨時調用。 它只是將listview重新綁定到游標。 使用所有棄用方法(requery())以及notifyDataSetChanged()的問題,我認為這是最簡單的方法。
請參考此鏈接...它的功能就像魅力一樣
更新SimpleCursorAdapter,同時在ListView中保持滾動位置
對於滾動的動態列表視圖我從數據庫添加了新項目..我在這里錯了..我為同一個simplecursoradapter每次分配新的適配器。 而不是創建新的適配器。 只是用
adapter.changecursor(newcursorValue);
adapter.notifydatasetChanged();
lsMedicine1.setSelectionFromTop(lsMedicine1.getLastVisiblePosition()-20, 0);
你需要調用swapcursor()
之前notifyDataSetChanged()
的適配器上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.