0

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?

1 Answer 1

1

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"); .you can use listarray instead of pass array to your adapter too and change or add is easy too. 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);
Sign up to request clarification or add additional context in comments.

3 Comments

just a questio @linux. Why passing List as last parameter of constructor I'm obtaining the following warning Type safety: The expression of type ArrayList needs unchecked conversion to conform to List<String> ??
I think because ArrayList extended List.sorry but I am not sure
Now it is ok. I was getting the the list from another method thet returned ArrayList instead of ArrayList<String>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.