我想用下面的办法:如何用新表格数据更新ListView?
protected void onCreate(Bundle savedInstanceState) {
...
recordsCursor = dbHelper.fetchRecords1();
startManagingCursor(recordsCursor);
String[] from = new String[]{DbAdapter.KEY_BL_SENDER, DbAdapter.KEY_BL_READ};
int[] to = new int[]{R.id.text1, R.id.background};
adapter = new SimpleCursorAdapter(this, R.layout.row, recordsCursor, from, to);
setListAdapter(adapter);
}
public boolean onOptionsItemSelected(MenuItem item) {
...
case R.id.list1:
recordsCursor = dbHelper.fetchRecords1();
String[] fromBL = new String[]{DbAdapter.KEY_BL_SENDER, DbAdapter.KEY_BL_READ};
int[] toBL = new int[]{R.id.text1, R.id.background};
Log.i(TAG, "count: "+recordsCursor.getCount()); // returns 475
adapter = new SimpleCursorAdapter(this, R.layout.row, recordsCursor, fromBL, toBL);
adapter.changeCursor(recordsCursor);
adapter.notifyDataSetChanged();
return true;
case R.id.list2:
recordsCursor = dbHelper.fetchRecords2();
String[] from = new String[]{DbAdapter.KEY_W_SENDER};
int[] to = new int[]{R.id.text1};
Log.i(TAG, "count: "+recordsCursor.getCount()); // returns 0
adapter = new SimpleCursorAdapter(this, R.layout.row, recordsCursor, from, to);
adapter.changeCursor(recordsCursor);
adapter.notifyDataSetChanged();
return true;
但它不更新列表。一旦显示来自fetchRecords1
的记录,它们不会被另一个表中的记录替换。我的代码有什么问题?
你在哪里归属这个适配器到你的列表? – Dyna
@Dyna,我已更新我的代码以反映它 –
@blahdiblah给予您的答案将解决您的问题! – Dyna