I'm using a ListView inside a fragment but I'm having trouble updating the ListView.
This is the code that I use for setting the adapter on my 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:
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:
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.
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. 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?