[done]notifyDataSetChanged() does not update ListActivity automatically

后端 未结 9 1748
甜味超标
甜味超标 2020-12-10 15:30

I\'ve got some troubles with notifyDataSetChanged() of a BaseAdapter. This method is called in refreshItems() and shall update the BaseAdapter of my ListActivity. On calling

相关标签:
9条回答
  • 2020-12-10 16:08

    The main reason behind this is the wrong reference of the adapter on which you are calling notifyDataSetChanged();

    I think you need to make sure that you are creating adapter object once and call notifyDataSetChanged() on the same object.

    You can debug the object reference value at creating time of adapter object and when you are calling notifyDataSetChanged() method.

    0 讨论(0)
  • 2020-12-10 16:11

    I had the same issue, and the solution for me was to call requestLayout() on the ListView.

    0 讨论(0)
  • 2020-12-10 16:13

    I encountered the same problem, and I tried to call notifyDataSetChanged() on the adapter. Besides, I also tried to call refreshDrawableState(), invalidateViews() on the view and none of those worked. All these methods are called in the UI thread. Then I found this How to clear the views which are held in the ListView's RecycleBin? . Finally setAdapter() worked, only if I create a new adapter.

    0 讨论(0)
  • 2020-12-10 16:19

    I think there may be some problems with the adapter; maybe it's not set.

    In my experience, there was always some kind of reason which prevented the listview (and adapter) to update.

    0 讨论(0)
  • 2020-12-10 16:20
    Why it works in first code ?
    

    --- Because you are setting the values to temp List and passing it the adapter and it shows it into listview.

    Why not work in second code ?
    

    --- Because you are setting temp to adapter far before you set value into temp second,your adapter class might not getting the updated value when you set new value to temp ..that because temp is not public or not at class level or not static.. Put the temp declaration at root level and try.

    And please show your full code as much as required and Logcat if you getting any warnings than also.

    0 讨论(0)
  • 2020-12-10 16:25

    I had the same problem (ListView updates only when i scroll it, even notifyDataSetChanged didn't help). i solve it this way: just try to do your "view modifications" in thread which creates your user interface i.e.

    activity.runOnUiThread(new Runnable() {         
            public void run() {
                  //do your modifications here
    
                  // for example    
                  adapter.add(new Object());
                  adapter.notifyDataSetChanged()  
            }
    });
    
    0 讨论(0)
提交回复
热议问题