Arrayadapter.Notifydatasetchanged() Is Not Working

ArrayAdapter.NotifyDataSetChanged() is not working?

notifyDataSetChanged() won't work for you. Reasons why

  1. Your adapter loses reference to your list.
  2. Always creating and adding a new list to the Adapter. Do like this:
    initialize the ArrayList while declaring globally.

ArrayList<MyItemType> myArrayList=new ArrayList<>(); // as a global variable

Add List to the adapter directly without checking null and empty condition. Set the adapter to the list directly(don't check for any condition)

myListView.setAdapter(adapter);


  1. Add data to the myArrayList Every time(if your data is completely new, then you can call adapter.clear() and myArrayList.clear() before actually adding data to the list) but don't set the adapter
    i.e If the new data is populated in the myArrayList, then just adapter.notifyDataSetChanged()

     adapter = new myListAdapter(getActivity(), 0, myArrayList);
  2. Remove this line

    adapter.setItems (myArrayList);

My adapter.notifyDataSetChanged() is not working

notifyDataSetChanged is "not working" because you are not changing the data set, you are creating a new copy, one that the adapter has no knowledge about. Use a List<> instead of an array, and modify that same list object that you passed to your adapter and it will work. If you want to keep your current code, then you have to create a new adapter over the new data set and apply it to your spinner.

ArrayAdapter notifyDataSetChanged not responding

I don't like how you're assuming the ArrayAdapter always keeps its reference to the same list you passed on the constructor. There are scenarios -- for example when you are using getFilter() -- that the ArrayAdapter will change its reference to a new list.

Change

items.remove(info.position);
listAdapter.notifyDataSetChanged();

to

listAdapter.remove(info.position);  // automatically calls notifyDataSetChanged

If that works, you should go through and modify the code everywhere you change the items list and assume the ArrayAdapter also changes under the hood.

Also keep in mind that ArrayList.remove() assumes the equals() method is defined correctly for your usage. You may want to check if your equals() implementation is causing heartburn.

ArrayAdapter does not update view when notifyDataSetChanged() is called

Note the comments by Selvin and Gautam while following this answer, as they are correct and will help you to understand why the code should look like the following:

floorList = new ArrayList<String>();
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, floorList);
arrayAdapter.setNotifyOnChange(true);
floorSpinner.setAdapter(arrayAdapter);
arrayAdapter.add("a"); //I would jsut pass this into the adapter's constructor

Call<ParkourResponse<ParkingPlaceResponseObject>> getPlaceDetailCall = Helper.getParkourService().getPlaceDetail(placeID);
getPlaceDetailCall.enqueue(new Callback<ParkourResponse<ParkingPlaceResponseObject>>() {
@Override
public void onResponse(Call<ParkourResponse<ParkingPlaceResponseObject>> call, Response<ParkourResponse<ParkingPlaceResponseObject>> response) {
ParkingPlaceResponseObject placeDetail = response.body().getRespObject();
Floor[] floorArray = placeDetail.getFloor();
arrayAdapter.clear(); //update the adapter, not the external list
arrayAdapter.setNotifyOnChange(false); //we'll only update after list is full, for efficiency
for(int i = 0; i < floorArray.length; i++){
arrayAdapter.add(Integer.toString(floorArray[i].getNumber()));
}
arrayAdapter.setNotifyOnChange(true);
arrayAdapter.notifyDataSetChanged();
}

@Override
public void onFailure(Call<ParkourResponse<ParkingPlaceResponseObject>> call, Throwable t) {

}
});

Array Adapter notifyDataSetChanged() will not work

Firstly I find the Android adapter implementation very flawed. When it comes to performing anything bespoke there seems to be ambiguous accounts of how to use it and the official documentation doesn't clarify any of them. I would be very happy to be proved wrong with this.

The way I got consistent results when editing data in the view was as follows:

  • All changes to the underlying data structure being presented should be done in an AsyncTask which makes sense as you are changing things on the UI Thread and don't want to have concurrency issues.

  • Operations on the underlying data structures should be performed by calling adapter methods so if you have a ListAdapter then you use the add, remove and clear of the list adapter. This means the adapter manages view notifications etc. This generally leads to having to create a custom adapter as the methods available are limited (there isn't even an add all in sdk versions before 7). You also end up with your adapter acting as a big fat controller, although I am aware we shouldn't be viewing android as an MVC pattern it still seems wrong.

  • I have created apps where I bypass adapter calls to operate on the underlying data structure and it has worked all through results ended up unpredictable unless you tightly managed notifications to the view. Now I just call through the adapter.

So although I am not able to explain why in notifiyDataSetChanged doesn't work specifically in your onClick Method. I am hopefully providing useful information which might help you to get your app working as expected.

adapter.notifyDataSetChanged() not working

Declare

private GraphView mGraphView;
private Spinner mSpinner;
private ArrayAdapter<String> adapter;

And remove parameter in method getDataFromApi and getDataForSpinner.

Recall getDataFromApi instead of adapter.notifyDataSetChanged();

Add adapter.notifyDataSetChanged(); in the end of onResponse in getDataFromApi



Related Topics



Leave a reply



Submit