我想在ViewPager
中展示唯一有限的页面。我使用以下查询从SQLite表中检索并获取计数行。
Cursor cursor2 = db.rawQuery("select count(activityObjId) from ActivityObjectList where " + "activityId" + " = ?", new String[]{strDescription});
我的count(activityObjId)
是2,所以我想在ViewPager
中添加2张图片。 count(activityObjId)
为5,因此我想在ViewPager
中添加5张图片。有人可以帮助解决这个问题。我试过但是收到了错误。
08-06 06:00:04.744 11207-11207/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
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.tazeen.classnkk:id/Image_ViewPagers Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.example.tazeen.classnkk.AllPosts_Page$MyPagerAdapter
at android.support.v4.view.ViewPager.populate(ViewPager.java:999)
at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15848)
at android.widget.ListView.setupChild(ListView.java:1918)
at android.widget.ListView.makeAndAddView(ListView.java:1843)
at android.widget.ListView.fillDown(ListView.java:675)
at android.widget.ListView.fillFromTop(ListView.java:736)
at android.widget.ListView.layoutChildren(ListView.java:1655)
at android.widget.AbsListView.onLayout(AbsListView.java:2012)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
我的活动代码
public void getActivityObjectId_Count() {
try {
dbhelper = new MyDbHelper(this);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor cursor2 = db.rawQuery("select count(activityObjId) from ActivityObjectList where " + "activityId" + " = ?", new String[]{strDescription});
if (cursor2 != null) {
cursor2.moveToFirst();
String strCount_ActivityObjectId = cursor2.getString(0);
intCount = Integer.parseInt(strCount_ActivityObjectId);
Log.e("Count", "in getActivityObjectId = " + strCount_ActivityObjectId);
Log.e("intCount convert ","to = " + intCount);
}
cursor2.close();
db.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
类MyPagerAdapter扩展了PagerAdapter {
Context mContent;
LayoutInflater mLayoutInflater;
private List<String> testImages = new ArrayList<String>();
private int[] GalImages = new int[]{
R.drawable.img_placeholder,
R.drawable.img_placeholder,
R.drawable.img_placeholder,
R.drawable.img_placeholder,
R.drawable.img_placeholder,
R.drawable.img_placeholder
};
public MyPagerAdapter(Context context) {
super();
mContent = context;
mLayoutInflater = (LayoutInflater) mContent.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public float getPageWidth(int position) {
return 0.9f;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated catch block
ImageView imageView = new ImageView(mContent);
imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public int getCount() {
return intCount;
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == ((ImageView) obj);
}
}
先谢谢。
答案 0 :(得分:0)
您似乎向ViewPager返回了错误的页数。您应该使用getCount()
方法返回实际页码。在这种情况下,您应该返回已添加到ViewPager
的图像数量。