Android - notifyDataSetChanged() not working

android, android-arrayadapter, java

Solution

An adapter keeps a copy of all the elements, so when you call notifyDataSetChanged, it checks its own copy of the elements

try the following:

@Override
protected void onResume() {
    super.onResume();
    dbData = getDbData();
    questions = getQuestions(1L);

    adapter.clear();
    adapter.addAll(questions);  // API level [1..10] use for(..){ adapter.add(question.get(index)) }  or simply implement addAll method in you custom adapter for your own convenience (thanks to @Mena)
    adapter.notifyDataSetChanged();
}

Problem

I have created a custom ArrayAdapter that displays a list of questions. I query the db, transform that data, pass the data to an array, and then pass the array to the ArrayAdapter to display my list. ``` dbData = getDbData(); List<Question> questions = getQuestions(1L); dbData.closeDb(); setContentView(R.layout.activity_questions); adapter = new QuestionsAdapter(this, getCurrentContext(), questions, 1L, dbData); ``` In my app, you can then click on the various question, manipulate some stuff and come back to the main list. The underlying array has not yet changed, because the changes have been persisted in the db. I again query the db, run my transformations, and then my array is up to date. I do this in my `onResume()` method, but the adapter won't recognize any changes with the `notifyDataSetChanged()` method. My `onResume()` looks like this: ``` @Override protected void onResume() { super.onResume(); dbData = getDbData(); questions = getQuestions(1L); adapter = new QuestionsAdapter(this, getCurrentContext(), questions, 1L, dbData); items.setAdapter(adapter); } ``` I have to query the db, make the transformations, create my array, and then create a new adapter for my changes to show up on the main list. Why can't I just do: ``` @Override protected void onResume() { super.onResume(); dbData = getDbData(); questions = getQuestions(1L); adapter.notifyDataSetChanges(); } ``` What do I need to do to make this work? Thanks for the help.

Original source