android:使用ListAdapter和SimpleCursorAdapter刷新ListView

spr*_*yan 3 android listview refresh listadapter simplecursoradapter

我正在尝试刷新使用创建为SimpleCursorAdapter的ListAdapter的ListView.

这是我在onCreate中创建Cursor和ListAdapter的代码,它填充了ListView.

tCursor = db.getAllEntries();       

ListAdapter adapter=new SimpleCursorAdapter(this,
                R.layout.row, tCursor,
                new String[] columns,
                new int[] {R.id.rowid, R.id.date});

setListAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

然后,我在另一个方法中向db添加一些数据,但我无法弄清楚如何刷新ListView.stackoverflow和其他地方的类似问题提到使用notifyDataSetChanged()和requery(),但ListAdapter或SimpleCursorAdapter的方法都没有.

spr*_*yan 5

我可以通过创建一个新的适配器并再次调用setListAdapter来刷新ListView.

我在另一种方法中将它命名为adapter2.

tCursor = db.updateQuery();       

ListAdapter adapter2=new SimpleCursorAdapter(this,
                R.layout.row, tCursor,
                columns,
                new int[] {R.id.rowid, R.id.date});

setListAdapter(adapter2);
Run Code Online (Sandbox Code Playgroud)

我不确定为什么这是必要的,但它现在有效.如果有人有更好的解决方案,我愿意尝试.