Java.lang.IllegalStateException: The application PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged android

前端 未结 6 1551
花落未央
花落未央 2021-02-08 10:00

I am trying to use a static class to pass value to a view instead of using intent as I have to pass a large amount of data. Sometimes I get this error and couldn\'t find out wha

相关标签:
6条回答
  • 2021-02-08 10:23

    look at your code:

     @Override
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }
    

    this code is use to refresh view when view change. it is also called notifyDataSetChanged()

    its optional ovverride method so you can remove it.

    Take a look at this answer: ViewPager PagerAdapter not updating the View

    or else you can Change the FragmentStatePagerAdapter to FragmentPagerAdapter

    0 讨论(0)
  • 2021-02-08 10:25
    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {}
    @Override
    public Parcelable saveState() {return null;}
    #Add in your adapter after getCount();
    
    0 讨论(0)
  • 2021-02-08 10:27

    I think your DataResult class is not necessary.

    According to your comment you are getting the error while you are updating the data set. I think you are getting data from some REST API or another web service. Then assign the data you gets from the API to a temporary Array List and update that temporary array list while you are getting new data. Don't touch the mListA variable until you finish receiving data. After complete getting data from the API assign the temporary array list you used to mListA directly in one line.

    mListA = tmpList;
    

    Then again call

    mList = mListA;
    pagerAdapter.notifyDataSetChanged();
    

    This should solve your error.

    0 讨论(0)
  • 2021-02-08 10:29

    It may because you first setAdapter() and notifyDataSetChanged in method initUI();

    Then, you instantiate the List<?> data in method initData(), the two methods in different lifetime of fragment.

    It's the problem about fragment's lifecycle

    0 讨论(0)
  • 2021-02-08 10:30

    Sometimes this error occurs.

    Here is my suggestion:

    In your DataResult->getData()

    return a copy of the data.

    DataResult is a singleton and holds the data. When your view gets the data, the view gets the data reference witch the DataResult is holding. Then setData() would change the data reference. Here comes the IllegalStateException.

    Hope it would work :)

    0 讨论(0)
  • 2021-02-08 10:33

    try using the following way:

    mList=new ArrayList<Datum>();
            mList.addAll(DataResult.getInstance().getData());
            DataResult.getInstance().resetData();
    
    0 讨论(0)
提交回复
热议问题