简体   繁体   English

Android ListView无法正确刷新

[英]Android listview not refreshing properly

I have a listview which will update fine only when a button is pressed. 我有一个列表视图,只有当按下按钮时,列表视图才会更新。 When the button is pressed, the listener calls a method which applies a new arrayadapter and the changes are shown. 当按下按钮时,侦听器调用一个方法,该方法将应用新的arrayadapter并显示更改。

However if I call the method without the invoke of a button, nothing happens until the fragment's lifecycle refreshes. 但是,如果我在不调用按钮的情况下调用该方法,则直到片段的生命周期刷新之前,什么都不会发生。

You shouldn't be applying a new array adapter every time the data changes. 您不应在每次数据更改时都应用新的阵列适配器。 What you should be doing is making mods to the array the adapter is pointing to, with out changing where the adapter is pointing, and then calling notifyDataSetChanged() . 您应该做的是对适配器所指向的数组进行修改,而notifyDataSetChanged()更改适配器所指向的位置,然后调用notifyDataSetChanged() see some other posts about this 查看其他有关此的帖子

https://stackoverflow.com/a/2708053/1116836 https://stackoverflow.com/a/2708053/1116836

https://stackoverflow.com/a/2250849/1116836 https://stackoverflow.com/a/2250849/1116836

http://androidadapternotifiydatasetchanged.blogspot.com/2013/02/android-notifydatasetchanged.html http://androidadapternotifiydatasetchanged.blogspot.com/2013/02/android-notifydatasetchanged.html

that last one could be very helpful as a guide/tutorial on how it should be done. 最后一篇作为应该如何做的指南/教程可能会非常有帮助。

Learning the best way to do this might take a bit of time, but once you figure it out it is a very easy and quick process doing it in other projects/areas from there on out. 学习最好的方法可能会花费一些时间,但是一旦弄清楚了,从头到尾在其他项目/区域中进行操作都是非常容易和快速的过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM