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
You can modify existing adapter data and call notifyDataSetChanged(). In your case you should call listView. setAdapter(adapter) in onClick method.
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 .
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();
}
Assuming the getNewData() function returns ArrayList<CustomItem>
, can you change the line:
items=getNewData();
to
items.addAll(getNewData());
and see if that works?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With