列表数据更改时更新 Android ListActivity?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1538796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:13:56  来源:igfitidea点击:

Update Android ListActivity when list data changes?

androidandroid-listviewlistactivity

提问by Simon

I wanted to know how to refresh a ListActivitywhen I change/add data to it.

我想知道如何ListActivity在更改/添加数据时刷新它。

I first thought that ListAdapterwould know when the list is changes, but when I have added elements to the list there is no update on the ListActivity, only when I close the ListActivity and reopen it again I see the changes occur.

我首先想到ListAdapter会知道列表何时更改,但是当我向列表添加元素时, 没有更新ListActivity,只有当我关闭 ListActivity 并再次重新打开它时,我才会看到更改发生。

So I searched for any method such as: update(), refesh()or something like that, but there is none. It seems I haven't gotten the concept, can someone help me please?

所以我搜索了任何方法,例如:update(), refesh()或类似的方法,但没有。好像没懂这个概念,有没有大神帮帮我?

回答by CommonsWare

If you are using an ArrayAdapter, manipulate the contents of the list through the adapter -- do not touch the underlying list. There are add(), insert(), and remove()methods on ArrayAdapter.

如果您使用的是ArrayAdapter,则通过适配器操作列表的内容——不要触及底层列表。还有add()insert()remove()上的方法ArrayAdapter

If you are using a CursorAdapter, run a requery()on the Cursor.

如果您使用的是CursorAdapter,请requery()Cursor.

回答by bhatt4982

Try ArrayAdapter:notifyDataSetChanged
Google docs link

尝试ArrayAdapter:notifyDataSetChanged
Google 文档链接

回答by No Name

Maybe you should do this setListAdapter(listAdapter);

也许你应该这样做 setListAdapter(listAdapter);