gpt4 book ai didi

java - PagerAdapter 问题

转载 作者:行者123 更新时间:2023-11-29 07:28:31 25 4
gpt4 key购买 nike

我尝试从 firebase 数据库加载数据库并得到这个异常。它是关于 notifyDataSetChanged 的。我试图在 SlideShowAapter 中放入 getCount() 但它没有用。

异常(exception)情况:

java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! 
Expected adapter item count: 0, found: 1 Pager id: com.example.dave.lomoapp:id/slideShow Pager class: class android.support.v4.view.ViewPager
Problematic adapter: class com.example.davenguyen.lomoapp.SlideShowAdapter
at android.support.v4.view.ViewPager.populate(ViewPager.java:1135)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:616)
at com.example.davenguyen.lomoapp.HomeFragment$1.run(HomeFragment.java:71)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这是我的 SlideShowAdapter

public class SlideShowAdapter extends PagerAdapter {

private boolean doNotifyDataSetChangedOnce = false;
private ArrayList<Genre> genresList;
private LayoutInflater inflater;
private Context context;

public SlideShowAdapter(Context context, ArrayList<Genre> images) {
this.context = context;
this.genresList =images;
inflater = LayoutInflater.from(context);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}

@Override
public int getCount()
{
if (doNotifyDataSetChangedOnce) {
doNotifyDataSetChangedOnce = false;
notifyDataSetChanged();
}
return genresList.size();
}

@Override
public Object instantiateItem(ViewGroup view, int position) {
View myImageLayout = inflater.inflate(R.layout.slide, view, false);
ImageView myImage = (ImageView) myImageLayout
.findViewById(R.id.slideShowImg);
Picasso.with(context).load(genresList.get(position).getImage()).into(myImage);
view.addView(myImageLayout, 0);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

}
});
return myImageLayout;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}

这是一个包含我的自定义适配器的 fragment

public class HomeFragment extends Fragment {

private static ViewPager mPager;
private static int currentPage = 0;

private ArrayList<Genre> genreList = new ArrayList<Genre>();

DatabaseReference mref;

public HomeFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v =inflater.inflate(R.layout.fragment_home, container, false);
return v;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);


loadImage();

mPager = (ViewPager) view.findViewById(R.id.slideShow);
mPager.setAdapter(new SlideShowAdapter(getActivity(), genreList));
CircleIndicator indicator = (CircleIndicator) view.findViewById(R.id.indicator);
indicator.setViewPager(mPager);

// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == genreList.size()) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 2500, 2500);
}

private void loadImage() {
// Get a reference to our genres
mref = FirebaseDatabase.getInstance().getReference().child("genres");
// Attach a listener to read the data at our genres reference
mref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
genreList.add (dataSnapshot.getValue(Genre.class));
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.i("READ_FAIELD","The read failed: " + databaseError.getCode());
}
});
}

提前致谢

最佳答案

我的猜测是问题出在您的适配器中标记 doNotifyDataSetChangedOnce。尝试删除 if 并在加载图像后调用 adapter.notifyDataSetChanged()(在 onDataChange 中)。

如果这解决了崩溃但 viewPager 没有更新,您可能需要在适配器中添加此代码:

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

关于java - PagerAdapter 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46685049/

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