1

I'm experiencing an issue with notifyOnDataSetChanged(). I am able to get my list view to load new data by creating a new CustomAdapter, but not by using notifyOnDataSetChanged().

private void repopulateListView(JSONObject resp) throws JSONException {
    arrayList.clear();
    List<MyObject> newArrayList = getData(); 
    arrayList.addAll(newArrayList); //I've checked that arrayList contains new data

    /* WORKS, new data is loaded in the list view */
    adapter = new CustomAdapter(MainActivity.this, arrayList);
    listView.setAdapter(adapter);
}

whereas the following doesn't work - the list view is not refreshed, it simply stays as it was before retrieving new data. I have checked that the new data is correctly retrieved.

private void repopulateListVIew(JSONObject resp) throws JSONException {
    arrayList.clear();
    List<MyObject> newArrayList = getData(); 
    arrayList.addAll(newArrayList); //I've checked that arrayList contains new data

    /* DOES NOT WORK, list view does not refresh with new data */
    ((BaseAdapter)adapter).notifyOnDataSetChanged();
}

My adapter was defined like so:

adapter = new CustomAdapter(MainActivity.this, arrayList);
listView = (ListView) findViewById(R.id.list_view); 
listView.setAdapter(adapter);
listView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //additional code
                    startActivity(DetailsActivity);
                }
            }
    );

UPDATE: @Sabeeh's answer worked. Based on answers from @Sabeeh and @ViniciusRodriguesLima below, I had to edit my CustomAdapter.java as follows to reference the list variable (in addition to adding the update() method):

public class CustomAdapter extends ArrayAdapter<MyObject> {
    private Context context;
    private List<MyObject> list = new ArrayList<>(); //add this variable

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inf = LayoutInflater.from(getContext());
        View customView = inf.inflate(R.layout.custom_beer_row, parent, false);
        //final MyObject chosenObj = getItem(position); Changed to the statement below
        final MyObject chosenObj = list.get(position); //this is CORRECT
        ...
    }

I have tried suggestions from the following pages to no avail:

Any help would be appreciated.

Community
  • 1
  • 1
VIN
  • 6,385
  • 7
  • 38
  • 77
  • please post you adapter code and you must update list in adapter and then call notifyDataSetChanged() – Sabeeh May 26 '16 at 18:49

2 Answers2

1

Please try this:

private void repopulateListVIew(JSONObject resp) throws JSONException {
    arrayList.clear();
    List<MyObject> newArrayList = getData();
    arrayList.addAll(newArrayList);
    adapter.update(arrayList);
}

and in your CustomAdapter class write following function

public void update(ArrayList<MyObject> list) {
    //replace arrayList variable with your class ArrayList variable
    this.arrayList = list;
    this.notifyDataSetChanged();
}

when you pass your data source object to the adapter, you just pass it by value. When you refresh your data in the Activity scope, your adapter doesn't know anything about it. That's why Sabeeh created an update method inside the adapter, this way he can updates its adapter data source. – Vinicius Rodrigues Lima

I hope this helps you.

Sabeeh
  • 1,478
  • 15
  • 15
  • Thanks @Sabeeh. Would you be able to explain why my code doesn't accomplish the same thing? (I've accepted your answer) – VIN May 26 '16 at 19:31
  • 1
    @Killiam, when you pass your data source object to the adapter, you just pass it by value. When you refresh your data in the Activity scope, your adapter doesn't know anything about it. That's why Sabeeh created an update method inside the adapter, this way he can updates its adapter data source. – Vinicius Lima May 26 '16 at 19:44
  • Thanks @ViniciusRodriguesLima. That makes a lot of sense. – VIN May 26 '16 at 19:50
0

Declare your adapter variable as ArrayAdapter and try using following code -

private void repopulateListVIew(JSONObject resp) throws JSONException {
    adapter.clear();
    List<MyObject> newArrayList = getData(); 
    adapter.addAll(newArrayList); 

    adapter.notifyOnDataSetChanged();
}
jaibatrik
  • 6,770
  • 9
  • 33
  • 62