Refreshing ArrayAdapter onResume [notifyDataSetChanged() not working]

后端 未结 2 677
我寻月下人不归
我寻月下人不归 2021-02-08 19:05

I am creating a contact list app using Fragments where one frag is a list of names in the contact list and the other is the rest of the details.

Here is the class that d

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-08 19:32

    notifyDataSetChanged() won't work for you. Reasons why

    Your adapter loses reference to your list. When you first initialize the Adapter it takes a reference of your arrayList and pass to its superclass. But if you reinitialize your existing arrayList it losts the reference hence the communication channel with Adapter :(.

    Always creating and adding a new list to the Adapter. Do like this:

    1. Initialize the arrayList while declaring globally.
    2. Add List to the adapter directly with out checking null and empty condition. Set the adapter to the list directly(don't check for any condition). Adapter gives you the guarantee that wherever you are changes the data of arrayList it will take care, but never loose the reference.
    3. Add data to the arrayList Every time(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()

    Keep trust to Documentations

提交回复
热议问题