带有SQlite的Android视图寻呼机

时间:2015-11-27 08:49:38

标签: android

我试图在视图寻呼机中显示图像,并将该寻呼机视为列表视图中的行。当我运行应用程序时,我的模拟器崩溃并收到错误。请在此处查看我的日志猫。

 java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 1, found: 4 Pager id: com.example.tazeen.classnkk:id/pagerMyPics Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.example.AdapterClasses.ViewPagerAdapter
            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:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1083)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
            at android.view.View.measure(View.java:17547)
            at android.widget.GridLayout.measureChildWithMargins2(GridLayout.java:1004)
            at android.widget.GridLayout.measureChildrenWithMargins(GridLayout.java:1014)
            at android.widget.GridLayout.onMeasure(GridLayout.java:1055)
            at android.view.View.measure(View.java:17547)
            at android.widget.ListView.setupChild(ListView.java:1944)
            at android.widget.ListView.makeAndAddView(ListView.java:1857)
            at android.widget.ListView.fillSpecific(ListView.java:1344)
            at android.widget.ListView.layoutChildren(ListView.java:1684)
            at android.widget.AbsListView.onLayout(AbsListView.java:2151)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:562)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
            at android.view.View.layout(View.java:15671)
            at android.view.ViewGroup.layout(ViewGroup.java:5038)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2114)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我不明白我的代码出错了。我在此imageArray中获取所有行图像并将此imageArray传递给holder.view_pager。

 if (!(vp_adapter == null))
                {
                    vp_adapter.notifyDataSetChanged();
                    holder.view_pager.invalidate();
                    vp_adapter = new ViewPagerAdapter(context, imageArray);

                    holder.view_pager.setAdapter(vp_adapter);
                    holder.view_pager.setCurrentItem(1);

                }
                else
                {
                    vp_adapter = new ViewPagerAdapter(context, imageArray);

                    holder.view_pager.setAdapter(vp_adapter);
                    holder.view_pager.setCurrentItem(1);

                }

这是ViewPager适配器类

public class ViewPagerAdapter extends PagerAdapter {
    // Declare Variables
    Context context;
    String png_Pattern = ".png";
    String jpg_pattern = ".jpg";
    String bmp_pattern = ".bmp";
    String gif_pattern = ".gif";
    String jpeg_pattern = ".jpeg";
    String mp3_Pattern = ".mp3";

    ArrayList<String> imgArray = new ArrayList<String>();
    LayoutInflater inflater;

    public ViewPagerAdapter(Context context, ArrayList<String> imgArray) {
        this.context = context;
        this.imgArray = imgArray;
    }

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

    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position)
    {
        ImageView imageView;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.horizontalitem , container,false);
        imageView = (ImageView) itemView.findViewById(R.id.image_AllPost);

        String strImage = imgArray.get(position);
        if (strImage.endsWith(mp3_Pattern))
        {
            imageView.setImageResource(R.drawable.audio_placeholder);
        }
        if (strImage.endsWith(png_Pattern) || strImage.endsWith(jpg_pattern) || strImage.endsWith(bmp_pattern) || strImage.endsWith(gif_pattern) || strImage.endsWith(jpeg_pattern))
        {
            imageView.setImageResource(R.drawable.img_placeholder);
        }
        ((ViewPager) container).addView(itemView);
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);

    }
}

有人可以帮我解决如何使用notifyDataSetChange解决此问题。

这是我的ListAdapter代码

 class MyListAdapter extends ArrayAdapter<All_Post> {
        Context context;
        int layoutResourceId;
        ArrayList<All_Post> data = new ArrayList<All_Post>();
        private DisplayImageOptions options;

        public MyListAdapter(Context context, int layoutResourceId, ArrayList<All_Post> data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;

            dbhelper = new MyDbHelper(context);
            dbhelper.onOpen(db);
        }

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

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View row, ViewGroup parent) {

            final Holder holder;
            if (row == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                row = vi.inflate(R.layout.allpostlist, parent, false);
                holder = new Holder();

                holder.text_ActivityId = (TextView) row.findViewById(R.id.textActivityId);
                holder.text_InspectorName = (TextView) row.findViewById(R.id.textInspectorName);
                holder.text_SpChar = (TextView) row.findViewById(R.id.textSpChar);
                holder.text_Notation = (TextView) row.findViewById(R.id.textNotation);
                holder.txtViewDate = (TextView) row.findViewById(R.id.txt_Date);
                holder.txtInitialLetter = (TextView) row.findViewById(R.id.txt_InitialLetter);
                holder.txtRemark = (TextView) row.findViewById(R.id.textRemark);

                holder.txtFollowUpCount = (TextView) row.findViewById(R.id.count_txtFollowUp);
                holder.txtCameraCount = (TextView) row.findViewById(R.id.count_txtImages);
                holder.txtAudioCount = (TextView) row.findViewById(R.id.count_txtAudio);

                holder.imgBtn_FollowUp = (ImageView) row.findViewById(R.id.imgButton_FoloowUp);
                holder.imgBtn_RateEquipMent = (ImageView) row.findViewById(R.id.imgButtonRateEquipment);
                holder.imgBtn_Camera = (ImageView) row.findViewById(R.id.imgButton_Camera);
                holder.imgBtn_Audio = (ImageView) row.findViewById(R.id.imgButton_RecordAudio);
               // holder.gallery = (Gallery)row.findViewById(R.id.gallery1);
                holder.view_pager = (ViewPager)row.findViewById(R.id.pagerMyPics);
                //holder.horizontalScrollView = (HorizontalScrollView) row.findViewById(R.id.hlist);
                //holder.lLinearLayout = (LinearLayout) row.findViewById(R.id.innerlay);

                row.setTag(holder);
            } else {
                holder = (Holder) row.getTag();
            }


            final All_Post all_Post = data.get(position);
            holder.text_ActivityId.setText(all_Post.getStrActivityId());
            holder.text_InspectorName.setText(all_Post.getStringInspectorname());

strListItem_ActivityId = all_Post.getStrActivityId();


            /*******************************************************Start******************************************************************/
            db = dbhelper.getReadableDatabase();
            Cursor cursor = db.rawQuery("select * from ActivityObjectList where activityId ='" + strListItem_ActivityId + "'", null);
            imageArray.clear();
            audioCount.clear();
            imageCount.clear();
            try {
                if (cursor.moveToFirst()) {
                    count = cursor.getCount();

                    do {
                        String imagePath = cursor.getString(cursor.getColumnIndex("imageaudioPath"));

                        if (imagePath.endsWith(mp3_Pattern))
                        {
                            String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
                            String path = baseDir + "/classnkk_audio/" + imagePath;
                            File file = new File(path);
                            long length = file.length();
                            length = length / 1024;

                            if (length > 0)
                            {
                                Log.e("", "");
                                imageArray.add(imagePath);
                                audioCount.add(imagePath);

                            }}

                        if (imagePath.endsWith(png_Pattern) || imagePath.endsWith(jpg_pattern) || imagePath.endsWith(bmp_pattern) || imagePath.endsWith(gif_pattern) || imagePath.endsWith(jpeg_pattern))
                        {
                            String strBaseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
                            String filepath = strBaseDir + "/classnkk_images/" + imagePath;

                            File file = new File(filepath);
                            long length = file.length();
                            length = length / 1024;

                            if (length > 0)

                            { imageArray.add(imagePath);
                                imageCount.add(imagePath);}
                        }
                    }
                    while (cursor.moveToNext());
                }
            } finally {
                if (cursor != null && !cursor.isClosed()) {
                    cursor.close();

                }
  if (!(vp_adapter == null))
                {
                    vp_adapter.notifyDataSetChanged();
                    holder.view_pager.invalidate();
                    vp_adapter = new ViewPagerAdapter(context, imageArray);

                    holder.view_pager.setAdapter(vp_adapter);
                    holder.view_pager.setCurrentItem(1);

                }
                else
                {
                    vp_adapter = new ViewPagerAdapter(context, imageArray);

                    holder.view_pager.setAdapter(vp_adapter);
                    holder.view_pager.setCurrentItem(1);

                }
 return row;
            }
        }

1 个答案:

答案 0 :(得分:0)

异常只是意味着getCount()发生了变化而你没有发送NotifyDataSetChanged。适配器会经常检查它,如果您读取数据库并且每次修改集合时都不通知适配器,那么您就不会收到此异常。

为了解决问题,在读取db之前实例化新列表。把它读到你的清单。 1.将新列表设置为适配器(使用通知适配器调用)或2.创建新适配器。