【问题标题】:Android - setListAdapter() + notifyDataSetChanged() clarificationAndroid - setListAdapter() + notifyDataSetChanged() 说明
【发布时间】:2014-06-26 07:07:16
【问题描述】:

我有一个ListFragment,其数据由自定义适配器填充(在我的情况下为SimpleAdapter)。我在扩展ListFragment 的班级中使用notifyDataSetChanged() 时遇到问题。经过大量的环顾和几篇(有用的)Stack Overflow 帖子之后:

listview not updating with notifydatasetchanged() call

Android ListView not refreshing after notifyDataSetChanged

adapters notifyDataSetChanged does not work

notifyDataSetChanged not working

ListView does not update when calling notifyDataSetChanged from a BaseAdapter

我知道一个松散的(并且非常不推荐的)解决方法是使用setListAdapter() 重新设置您的适配器。但是,我现在也面临着这个问题。

文档http://developer.android.com/reference/android/app/ListFragment.html#setListAdapter(android.widget.ListAdapter) 提到setListAdapter()

为列表视图提供光标。

但我还有一些问题。

第一季度。使用 setListAdapter() 'point' 多次初始化适配器是否指向同一个适配器实例?

第二季度。当使用 setListAdapter() 多次设置适配器时,调用 getListAdapter() 然后调用 notifyDataSetChanged() 实际会发生什么?

第三季度。这个问题基于 Q2 的假设——当多次设置适配器时调用 notifyDataSetChanged() 时,如果它们存在,哪些适配器实例(这部分是假设)实际上被通知更改?

我是 Android 的初学者,我相信有很多细微差别我不明白。如果您能澄清这些问题,我将不胜感激。也非常感谢您抽出宝贵的时间。

【问题讨论】:

    标签: android android-listview android-adapter


    【解决方案1】:

    第一季度。是否使用 setListAdapter() 'point' 多次初始化适配器 同一个适配器实例?

    Ans: 初始化适配器将仅指向您使用 setListAdapter 设置的最后一个实例。

    Q2. What actually happens when a call is made to getListAdapter() and then to 
    notifyDataSetChanged() when an adapter has been set multiple times using 
    setListAdapter() ?
    

    Ans: 不管你初始化了多少个适配器,使用getListAdapter() 只会检索最后一个实例。当你使用notifyDataSetChanged() 时,只有使用getListAdapter() 检索的最后一个实例将刷新即;最后一个实例将被重新加载(通过调用getView)。

    Q3. This question is based on an assumption from Q2- when notifyDataSetChanged() is 
    called when an adapter is set multiple times, which of those adapter instances (this 
    part is the assumption), if they exist' is actually being notified for change ?
    

    Ans:上面的答案包含了对此的解释。

    【讨论】:

    • 你能改写以下句子吗? 当您使用 notifyDataSetChanged() 时,只会刷新最后一个实例,即;最后一个实例将被重新加载(通过调用 getView),
    • 请检查以上是否正常
    猜你喜欢
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 2019-03-22
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多