Android: notifyDataSetChanged(); not working

后端 未结 8 1578
旧时难觅i
旧时难觅i 2020-11-28 10:33

I have a database in a server and from a Tablet I take some values from one table in the database. I load this information correctly into a list but I would like to know why

8条回答
  •  广开言路
    2020-11-28 11:12

    One of the main reasons notifyDataSetChanged() won't work for you - is,

    Your adapter loses reference to your list.

    When you first initialise the Adapter it takes a reference of your arrayList and passes it to its superclass. But if you reinitialise your existing arrayList it losses the reference, and hence, the communication channel with Adapter.

    When creating and adding a new list to the Adapter. Always follow these guidelines:

    1. Initialise the arrayList while declaring it globally.
    2. Add the List to the adapter directly with out checking for null and empty values . Set the adapter to the list directly (don't check for any condition). Adapter guarantees you that wherever you make changes to the data of the arrayList it will take care of it, but never loose the reference.
    3. Always modify the data in the arrayList itself (if your data is completely new than you can call adapter.clear() and arrayList.clear() before actually adding data to the list) but don't set the adapter i.e If the new data is populated in the arrayList than just adapter.notifyDataSetChanged()

    Stay true to the Documentation.

提交回复
热议问题