gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:00:15 35 4
gpt4 key购买 nike

我正在构建一个简单的图库应用。图片与服务器同步。现在我有一个简单的 gridView 显示图像,onClick 图像在 viewPager 中打开。就像 android 默认图库一样。

问题是-

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

我需要一种方法来从后台通知适配器 viewPager。为了在后台加载数据,我使用 Koush/Ion

CLass ImageGrid (summary)

public class ImageGrid extends Fragment implements HttpConnectCallback, FutureCallback<JsonArray> {
...
..
.
gridView.setOnItemClickListener(CustomListener);
//onClick-> the following code executes

PagerFragment pagerFragment = new PagerFragment().newInstance(position);
...
..
.

当 PagerFragment 处于 Activity 状态时,数据加载仍在后台继续

Class PagerFragment (summary)

public class PagerFragment extends Fragment {
...
..
.
viewPager = (ViewPager) view.findViewById(R.id.splash_pager);

viewPager.setAdapter(new PagerAdapter);// a PagerAdapter

现在,两个类中的适配器图像源相同。即我有一个 static ArrayList<String>包含磁盘上图像的路径。

现在如果列表中的数据发生变化并且我尝试在发生异常后滚动 viewPager

java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged!
at android.support.v4.view.ViewPager.populate(Unknown Source)
at android.support.v4.view.ViewPager.populate(Unknown Source)
at android.support.v4.view.ViewPager.smoothScrollTo(Unknown Source)
at android.support.v4.view.ViewPager.scrollToItem(Unknown Source)
at android.support.v4.view.ViewPager.setCurrentItemInternal(Unknown Source)
at android.support.v4.view.ViewPager.onTouchEvent(Unknown Source)
...
...
...

最佳答案

使用 clone() 方法从我传递给构造函数的 ArrayList 复制数据ViewPager的adapter终于解决了这个问题。

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

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