简体   繁体   English

使用ArrayAdapter更新ListView

[英]Updating ListView using ArrayAdapter

I'm using a ListView inside a fragment but I'm having trouble updating the ListView. 我在fragment使用了ListView但是无法更新ListView。

This is the code that I use for setting the adapter on my ListView 这是我用于在ListView上设置适配器的代码

    adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_1,
            ((TabNavigation)this.getActivity()).getItems());
    setListAdapter(adapter);

where getItems() is a getter for the String array, named items: 其中getItems()是String数组的获取器,名为items:

items= new String[]{"item 1", "item 2", "item 3", "item 4", "item 5", "item 6"};

At this point all works good: I can see in my list the six item. 在这一点上,一切都很好:我可以在列表中看到这六个项目。

Now, if I try to update my array in this way: 现在,如果我尝试以这种方式更新阵列:

items[0]="injected item";

and than I call notifyDataSetChanged() over my adapter all works finem, because the item in my list change correctly, showing as first element the String injected item instead of item 1. But, if instead of that instruction I use the following: 然后我通过适配器调用notifyDataSetChanged()都可以正常工作,因为列表中的项正确更改,将第一个元素显示为String注入的项而不是项1。但是,如果使用该指令,则使用以下命令:

items= new String[]{"injected item", "item 2", "item 3", "item 4", "item 5", "item 6"};

and than I call the notifyDataSetChanged() over my adapter, the list is not update. 并且比我通过适配器调用notifyDataSetChanged() ,列表未更新。

I think this depends on the passage of value for reference. 我认为这取决于参考价值的传递。 Seems like the adapter continues to refer to the copy of array passed when I setted the adapter first time. 好像适配器继续引用我第一次设置适配器时传递的数组副本。 In fact if I use : 实际上,如果我使用:

temp= new String[]{"injected item", "item 2", "item 3", "item 4", "item 5", "item 6"};      
System.arraycopy(temp, 0, items, 0, temp.length);

the listView is update correctly. listView已正确更新。 But I must use exactly the same lenght of the original array. 但是我必须使用与原始数组完全相同的长度。

So the actual question is: how can I update the ListView if I need to change the number of the items inside the list? 因此,实际的问题是:如果需要更改列表中的项目数,如何更新ListView

you can use ListArray too.adapter get a list or array object for display its items.you can set any array or list to it and anytime you need change items you can change it array but array have lenght of constant if you want change number of items of array you should use adapter.add("string"); 您也可以使用ListArray.adapter获取用于显示其项目的列表或数组对象。您可以为其设置任何数组或列表,并且每当您需要更改项目时,都可以更改它,但是如果要更改编号,则array具有恒定的长度您应该使用adapter.add("string");的数组项adapter.add("string"); .you can use listarray instead of pass array to your adapter too and change or add is easy too. 您也可以使用listarray而不是将数组传递给适配器,并且更改或添加也很容易。 this code may help you 此代码可能会帮助您

final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
    list.add(values[i]);
}
final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);

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

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