Android refreshing adapter work after rotation again device

前端 未结 3 1124
[愿得一人]
[愿得一人] 2020-12-18 23:09

This code works fine when i add some data into \'List model\' and restore saved data on rotation device, unfortunately after restore data and set that to model

相关标签:
3条回答
  • 2020-12-18 23:48

    My problem resolved,

    after adding this below line into manifest for specific activity where you setAdapter

    android:configChanges="screenSize|orientation|screenLayout" 
    

    But it will return best view if you design Both Portrait and Landscape layout or running app only in one mode of these both.

    0 讨论(0)
  • 2020-12-18 23:53

    I tried your code and it works just fine with few updates. When you perform this action

     model.add(temp);
     adapter.notifyItemInserted(model.size() - 1);
    

    You are assuming the persistence of the link between the object model and the adapter datasource. This assumption is wrong, in fact if you just replace the code above with

     model.add(temp);
     adapter.setData(model);
     adapter.notifyItemInserted(model.size() - 1);
    

    it works fine. I hope it helped.

    0 讨论(0)
  • 2020-12-19 00:06
    The main reason is that, because you called set-data and that method did not immediately notify that the data set was changed, therefore it was not ware.
    
          public void setData(List<RobotViewModel> data) {
                Log.e("data size ", data.size() + "");
                list.clear();
                list.addAll(data);
            }
    
         public void setData(List<RobotViewModel> data) {
                Log.e("data size ", data.size() + "");
    
                list.clear();
                notifyDataSetChanged(); //here,to signal change occurred.
                list.addAll(data);
                notifyDataSetChanged(); //here,to signal change occurred.
    
            }
    
    should probably get into the habit of notifying changes on the spot.
    

    The main problem is this, you never notified the changes you made, you cleared it , but you never told it, you know, bu the adapter does not know, also you inserted fresh data with addAll but you still did not notify it.

    0 讨论(0)
提交回复
热议问题