Update data in Arrayadapter

2019-06-15 04:50发布

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

2条回答
我命由我不由天
2楼-- · 2019-06-15 05:39

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

items=getNewData();

to

items.addAll(getNewData());

and see if that works?

查看更多
趁早两清
3楼-- · 2019-06-15 05:40

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();
            }
查看更多
登录 后发表回答