机器人:刷新的ListView使用ListAdapter和SimpleCursorAdapter机器人、ListView、SimpleCursorAdapter、ListAdapter

2023-09-06 22:15:36 作者:抱歉丶你不是我的唯一

我试图刷新使用创建为SimpleCursorAdapter一个ListAdapter一个ListView。

I'm trying to refresh a ListView that uses a ListAdapter created as a SimpleCursorAdapter.

下面是我的$ C $下创建光标和ListAdapter中的onCreate其中填充的ListView。

Here is my code for creating the Cursor and ListAdapter in onCreate which populates the 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);

然后,我的一些数据添加到另一种方法分贝,但我无法弄清楚如何刷新的ListView。在计算器和其他地方类似的问题提用notifyDataSetChanged(),并重新查询(),但也不是ListAdapter或SimpleCursorAdapter的方法。

Then, I add some data to the db in another method, but I can't figure out how to refresh the ListView. Similar questions on stackoverflow and other places mention using notifyDataSetChanged() and requery(), but neither are methods of ListAdapter or SimpleCursorAdapter.

推荐答案

我能够得到的ListView通过创建一个新的适配器,并再次呼吁setListAdapter刷新。

I'm able to get the ListView to refresh by creating a new adapter and calling setListAdapter again.

我把它命名为适配器2中的另一种方法。

I named it adapter2 in the other method.

tCursor = db.updateQuery();       

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

setListAdapter(adapter2);

我不知道为什么这是必要的,但它适用于现在。如果任何人有一个更好的解决方案,我愿意尝试一下。

I'm not sure why this is necessary, but it works for now. If anyone has a better solution, I'm willing to try it.