Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update data in Arrayadapter

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

like image 822
Ant Avatar asked Jul 25 '12 21:07

Ant


People also ask

How do I refresh ArrayAdapter?

You can modify existing adapter data and call notifyDataSetChanged(). In your case you should call listView. setAdapter(adapter) in onClick method.

What is the use of ArrayAdapter in Android?

You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .


2 Answers

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();
            }
like image 187
metalwihen Avatar answered Sep 21 '22 08:09

metalwihen


Assuming the getNewData() function returns ArrayList<CustomItem>, can you change the line:

items=getNewData();

to

items.addAll(getNewData());

and see if that works?

like image 44
azgolfer Avatar answered Sep 18 '22 08:09

azgolfer