Android Array Adapter with ArrayList and ListView not updating when the arraylist is changed
Asked Answered
M

3

12

I have an android app with a screen that comprises of a ListView, which I am using to display a list of devices. These devices are held in an array.

I am trying to use the ArrayAdapter to display what is in the array on the screen in a list.

It works when I first load the SetupActivity class, however, there is the facility to add a new device in the addDevice() method, which means the array holding the devices is updated.

I am using notifyDataSetChanged() which is supposed to update the list but it doesn't seem to work.

public class SetupActivity extends Activity
{   
    private ArrayList<Device> deviceList;

    private ArrayAdapter<Device> arrayAdapter;

    private ListView listView;

    private DevicesAdapter devicesAdapter;

    private Context context;

    public void onCreate(Bundle savedInstanceState)  //Method run when the activity is created
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.setup);  //Set the layout

        context = getApplicationContext();  //Get the screen

        listView = (ListView)findViewById(R.id.listView);

        deviceList = new ArrayList<Device>();

        deviceList = populateDeviceList();  //Get all the devices into the list

        arrayAdapter = new ArrayAdapter<Device>(this, android.R.layout.simple_list_item_1, deviceList);

        listView.setAdapter(arrayAdapter);  
    }

    protected void addDevice()  //Add device Method (Simplified)
    {
        deviceList = createNewDeviceList();    //Add device to the list and returns an updated list

        arrayAdapter.notifyDataSetChanged();    //Update the list
}
}

Can anyone see where I am going wrong?

Moffit answered 7/11, 2012 at 17:27 Comment(0)
R
39

For an ArrayAdapter, notifyDataSetChanged only works if you use the add, insert, remove, and clear functions on the Adapter.

  1. Use clear to clear the adapter - arrayAdapter.clear()
  2. Use Adapter.addAll and add the newly formed list - arrayAdapter.addAll(deviceList)
  3. Call notifyDataSetChanged

Alternatives:

  1. Repeat this step after new devicelist is formed - but this is redundant

    arrayAdapter = new ArrayAdapter<Device>(this, android.R.layout.simple_list_item_1, deviceList);
    
  2. Create your own class derived from BaseAdapter and ListAdapter that gives you more flexibility. This is most recommended.
Rabbet answered 7/11, 2012 at 18:2 Comment(1)
this is not true, see @Laura responseMiscegenation
M
12

While the accepted answer solves the problem, the explanation of why is incorrect, and since this is an important concept I thought I'd attempt to clarify.

Slartibartfast's explanation that notifyDataSetChanged() only works when add, insert,remove, or clear is called on the adapter is incorrect.

That explanation is true of the setNotifyOnChange() method, which if set to true (as it is by default) will automatically call notifyDataSetChanged() when any of those four actions occur.

I think the poster confused the two methods. notifyDatasetChanged() itself does not have those restrictions. It just tells the adapter that the list it is looking at has changed, and it does not matter how the change to the list actually happened.

While I can't see the source code for your createNewDeviceList(), I would guess your problem came from the fact that you had the adapter referencing the original list you created, and then you created a new list in createNewDeviceList(), and since the adapter was still pointing to the old list it could not see the changes.

The solution slartibartfast mentioned works because it clears the adapter and specifically adds the updated list to that adapter. Thus you don't have the problem of your adapter pointing to the wrong place.

Hope this helps someone!

Mighty answered 5/9, 2014 at 6:34 Comment(0)
S
0

your method addDevice is causing an endless loop. Dont call a method from itself like you are doing here :

deviceList = addDevice();
Sliwa answered 7/11, 2012 at 17:30 Comment(2)
bother, that was just a typo on my part... sorry for the confusionMoffit
can you post your createNewDeviceList(), chances are its not adding anything new to your list and creating the new list with the same elements and order in the originalSliwa

© 2022 - 2024 — McMap. All rights reserved.