Failing to update ListView even after calling notifyDataSetChanged()

101 Views Asked by At

The code is simple, I just wanted to see how to update the listview with entirely new data. I am storing values in a array and passing it to the arrayadapter. When clicked on a listview item, I want clear the exiting listview and add a new set of values and display the new items in the list view.

I can see that the array is getting updated with new values, but these values do not get displayed even after calling notifyDataSetChanged(). Please take a look at the code and do let me know what I am doing wrong here. Thank you in advance.

public class MainActivity extends Activity {
   public ListView mListView;
   String[] stringArray;
   string[] stringArray1;
   String[] stringArray2;
   String[] stringArray3;
   String[] stringArray4;
   static int track=1;
   public ArrayAdapter<String> adapter;
   public ArrayList<String[]> aList;
   static Iterator<String[]> aitr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mListView=(ListView) findViewById(R.id.listView1);

    //stringArray= new String[4];
    stringArray1 = new String[]  {"Bright Mode1", "Normal Mode1","silly mode1", "dupe mode1","trail mode1"};
    stringArray2 = new String[] { "Bright Mode2", "Normal Mode2","silly mode2", "dupe mode2","trail mode2"};
    stringArray3 = new String[] { "Bright Mode3", "Normal Mode3","silly mode3", "dupe mode3","trail mode3"};
    stringArray4 = new String[] { "Bright Mode4", "Normal Mode4","silly mode4", "dupe mode4","trail mode4"};
    aList=new ArrayList<String[]>();
    aList.add(stringArray2);
    aList.add(stringArray3);
    aList.add(stringArray4);
    aitr=aList.iterator();

    stringArray=stringArray1;
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
    mListView.setAdapter(adapter);
}

public void updateArray(){
    if(aitr.hasNext()){
        mListView.clearChoices();
        stringArray=aitr.next();
    }
}

@Override
protected void onStart(){
    super.onStart();
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int a = view.getId();

            Log.e("devsample", "the is is: " + a + ", " + position + ", " + id);
            updateArray();
            adapter.notifyDataSetChanged() ;
        }
    });
}

}

2

There are 2 best solutions below

0
On BEST ANSWER

So I solved the problem with a mix of Questioner's answer and the explanation provided by st0le in this post Unable to modify ArrayAdapter in ListView: UnsupportedOperationException

2
On

In updateArray() method you are assigning new array you your stringArray but your adapter still keeps reference to your old stringArray. Add these 2 lines at the end of your updateArray() to update your adapter:

adapter.clear();
adapter.addAll(stringArray); // or if you're targeting Android < 11 call: adapter.add() in a loop