gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:39 28 4
gpt4 key购买 nike

我正在尝试使用静态类将值传递给 View ,而不是使用 intent,因为我必须传递大量数据。有时我会得到这个错误,但找不到主要原因是什么

错误:-

java.lang.IllegalStateException:应用程序的 PagerAdapter 在未调用 PagerAdapter#notifyDataSetChanged 的​​情况下更改了适配器的内容!预期适配器项数:101,找到:200

我的寻呼机类

public class MemeDetailActivity extends AppCompatActivity implements OnDialogClickListner<Void> {
private ViewPager mPager;
private List<Datum> mList;
private ScreenSlidePagerAdapter pagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meme_detail);
mList = DataResult.getInstance().getData();
DataResult.getInstance().resetData();
if (mList != null)
startPager(selected_position);
else
Toast.makeText(this, "Error loading data", Toast.LENGTH_SHORT).show();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

private void startPager(int position) {
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(pagerAdapter);
pagerAdapter.notifyDataSetChanged();
mPager.setOffscreenPageLimit(0);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
new Interactors(MemeDetailActivity.this).updateView(Util.getAccessToken(), mList.get(position).getId(), new OnFinishListner<ViewsRM>() {
@Override
public void onFinished(ViewsRM result) {

}

@Override
public void onFailed(String msg) {
}
});
}

@Override
public void onPageScrollStateChanged(int state) {

}
});
mPager.setCurrentItem(position);
}

public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}


//todo entry point 3 : showing the image in the viewpager
@Override
public Fragment getItem(int position) {
Fragment fragment = new fragment_mypager_new();
Bundle bundle = new Bundle();
if (frmMyMemes)
bundle.putParcelable("MY_DATA", myList);
bundle.putSerializable("DATA", mList.get(position));
fragment.setArguments(bundle);
return fragment;
}

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

@Override
public int getCount() {
return mList.size();
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}

public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}

}

我的静态类

public class DataResult<T> {

private static DataResult instance;
private List<T> data = null;

protected DataResult() {

}

public static synchronized DataResult getInstance() {
if (instance == null) {
instance = new DataResult();
}
return instance;
}

public List<T> getData() {
return data;
}

public void setData(List<T> data) {
this.data = data;
}

public void resetData() {
data = null;
}
}

我如何调用 Activity

    Intent intent = new Intent(getActivity(), MemeDetailActivity.class);
DataResult.getInstance().setData(mListA);

最佳答案

看看你的代码:

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

此代码用于在 View 更改时刷新 View 。它也被称为 notifyDataSetChanged()

它是可选的 ovverride 方法,因此您可以将其删除。

看看这个答案:ViewPager PagerAdapter not updating the View

否则您可以将 FragmentStatePagerAdapter 更改为 FragmentPagerAdapter

关于Java.lang.IllegalStateException : The application PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged android 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41547995/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com