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
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
@Override
public void restoreState(Parcelable state, ClassLoader loader) {}
@Override
public Parcelable saveState() {return null;}
#Add in your adapter after getCount();
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.
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
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 :)
try using the following way:
mList=new ArrayList<Datum>();
mList.addAll(DataResult.getInstance().getData());
DataResult.getInstance().resetData();