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

前端 未结 3 441
一个人的身影
一个人的身影 2021-02-04 10:52

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

3条回答
  •  旧时难觅i
    2021-02-04 11:28

    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(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.

提交回复
热议问题