java 在 Arrayadapter 中更新数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11658758/
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
Update data in Arrayadapter
提问by Ant
i have this problem, i have
我有这个问题,我有
private ArrayList<CustomItem> items;
private ArrayAdapter<CustomItem> arrayAdapter;
i show the data present in items, this data i see in listview, now i want to update data and see this new data
我显示项目中存在的数据,这个数据我在列表视图中看到,现在我想更新数据并查看这个新数据
if (!items.isEmpty()) {
items.clear(); // i clear all data
arrayAdapter.notifyDataSetChanged(); // first change
items = getNewData();// insert new data and work well
arrayAdapter.notifyDataSetChanged(); // second change
}
in the first change i see the data are cleaned, but in second change i don't see the new data in listview, i check and the item don't empty
在第一个更改中,我看到数据已被清除,但是在第二个更改中,我在列表视图中没有看到新数据,我进行了检查,但该项目不为空
i don't know where is the error, can you help me? best regads Antonio
我不知道错误在哪里,你能帮我吗?最好的问候安东尼奥
采纳答案by azgolfer
Assuming the getNewData() function returns ArrayList<CustomItem>
, can you change the line:
假设 getNewData() 函数返回ArrayList<CustomItem>
,您可以更改该行:
items=getNewData();
to
到
items.addAll(getNewData());
and see if that works?
看看这是否有效?
回答by metalwihen
This is how I update the Adapter with new data:
这就是我用新数据更新适配器的方式:
if (arrayAdapter == null) {
arrayAdapter = new CustomArrayAdapter(getActivity(), data);
listview.setAdapter(userAutoCompleteAdapter);
} else {
arrayAdapter.clear();
arrayAdapter.addAll(newData);
arrayAdapter.notifyDataSetChanged();
}
回答by Sam Chen
The ArrayList
you created in Activity
class is an reference variable, which is holding the same reference to the ArrayList
in your Adapter
class by passing through the constructor (when you initialize the Adapter
object).
在ArrayList
你创建的Activity
类是一个引用变量,其持有相同的参考ArrayList
你Adapter
穿过构造类(在初始化的Adapter
对象)。
However, by executing items = getNewData()
, you're assigning a new reference to the items
in your Activity
class, the reference in your Adapter
class remains the same, thus you see no changes on the screen.
然而,通过执行items = getNewData()
,你分配一个新的参考items
你的Activity
类,在您的参考Adapter
类仍然是相同的,所以你在屏幕上看到任何改变。
It's like this:
就像这样:
personA: the ArrayList object in Activityclass
personA:Activity类中的 ArrayList 对象
personB: the ArrayList object in Adapterclass
personB:Adapter类中的 ArrayList 对象
PersonA and personB they are both holding a map of USA (separately), and the screen shows personB's map. Then someone replaces the map of personA with another coutry's map. Guess what, the screen still shows USA map.
PersonA 和personB 他们都拿着一张美国地图(分别),屏幕上显示了personB 的地图。然后有人用另一个国家的地图替换了 personA 的地图。你猜怎么着,屏幕上仍然显示美国地图。
Instead of changing the reference of items
, you should use add()
, remove()
, clear()
or addAll()
to modify the items
's data then call notifyDataSetChanged()
.
而不是改变的参考items
,你应该使用add()
,remove()
,clear()
或addAll()
修改items
的数据,然后调用notifyDataSetChanged()
。