在Android中使用PagerAdapter的notifyDataSetChanged()

时间:2015-08-07 10:01:07

标签: android

我将视图寻呼机作为行放在列表视图中,并且在滚动列表视图时想要更新视图寻呼机的内容。我试过=

 @Override
        public int getCount() {

            // return PAGE_NUM;
            Log.e("intCount8888: "," = " +intCount );
            notifyDataSetChanged();
            return intCount;

        }

我在getView()

中也调用了列表适配器上的notifyDataSetChange
  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        Holder holder = null;
        if (row == null)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new Holder();
            holder.txt_Descid = (TextView) row.findViewById(R.id.textDescid);
            holder.txtTitle = (TextView) row.findViewById(R.id.txt_Desc);
            holder.txtInitialLetter = (TextView) row.findViewById(R.id.txt_InitialLetter);
            holder.txtRemark = (TextView) row.findViewById(R.id.textRemark);

            holder.imgBtn_Rework = (ImageView) row.findViewById(R.id.imgButtonRework);
            holder.imgBtn_FollowUp = (ImageView) row.findViewById(R.id.imgButtonFoloowUp);
            holder.imgBtn_RateEquipMent = (ImageView) row.findViewById(R.id.imgButtonRateEquipment);
            holder.imgBtn_Camera = (ImageView) row.findViewById(R.id.imgButtonCamera);
            holder.imgBtn_Audio = (ImageView) row.findViewById(R.id.imgButtonAudio);
            holder.imgBtn_FollowInspector = (ImageView) row.findViewById(R.id.imgButtonFollowInspector);

            holder.viewPager = (ViewPager) row.findViewById(R.id.Image_ViewPagers);

            holder.viewPager.setPageMargin(-50);
            holder.viewPager.setHorizontalFadingEdgeEnabled(true);
            holder.viewPager.setFadingEdgeLength(20);
            holder.viewPager.setClipToPadding(false);
            holder.viewPager.setPadding(-5, 0, 0, 0);
            MyPagerAdapter adapter = new MyPagerAdapter(getContext());

            holder.viewPager.setAdapter(adapter);
            holder.viewPager.setCurrentItem(0);
            adapter.notifyDataSetChanged();
            //holder.viewPager.getAdapter().notifyDataSetChanged();


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

        All_Post all_Post = data.get(position);


        holder.txt_Descid.setText(Integer.toString(all_Post.getID()));
        String strTextDescId = holder.txt_Descid.getText().toString().trim();
        Log.e("strTextDescId ", " = " + strTextDescId);
        getActivityId(strTextDescId);
        getActivityObjectId_Count();

        holder.txtTitle.setText(all_Post.getName());
        String strTxtTitle = holder.txtTitle.getText().toString().trim();
        Log.e("strTxtTitle ", " = " + strTxtTitle);


        holder.txtInitialLetter.setText(all_Post.getInitName());
        holder.txtRemark.setText(all_Post.getStrDesc());

        holder.imgBtn_Rework.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("Button click ", "event is fire");

            }
        });

但是收到错误

08-07 05:56:27.806    4475-4475/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.StackOverflowError
            at java.lang.IntegralToString.convertInt(IntegralToString.java:209)
            at java.lang.IntegralToString.appendInt(IntegralToString.java:173)
            at java.lang.StringBuilder.append(StringBuilder.java:139)
            at com.example.tazeen.classnkk.AllPosts_Page$MyPagerAdapter.getCount(AllPosts_Page.java:553)
            at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:882)
            at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2905)
            at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
            at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
            at com.example.tazeen.classnkk.AllPosts_Page$MyPagerAdapter.getCount(AllPosts_Page.java:554)
            at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:882)
            at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2905)
            at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
            at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
            at com.example.tazeen.classnkk.AllPosts_Page$MyPagerAdapter.getCount(AllPosts_Page.java:554)
            at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:882)
            at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2905)
            at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
            at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
            at com.example.tazeen.classnkk.AllPosts_Page$MyPagerAdapter.getCount(AllPosts_Page.java:554)
            at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:882)
            at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2905)
            at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
            at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
            at com.example.tazeen.classnkk.AllPosts_Page$MyPagerAdapter.getCount(AllPosts_Page.java:554)
            at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:882)
            at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2905)
            at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
            at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
            at com.example.tazeen.classnkk.AllPosts_Page$MyPagerAdapter.getCount(AllPosts_Page.java:554)
            at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:882)
            at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2905)
            at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
            at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
            at com.example.tazeen.classnkk.AllPosts_Page$MyPagerAdapter.getCount(AllPosts_Page.java:554)
            at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:882)
            at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2905)
            at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
            at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
            at com.example.tazeen.classnkk.AllPosts_Page$MyPagerAdapter.getCount(AllPosts_Page.java:554)
            at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:882)
            at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2905)
            at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
            at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
            at com.example.tazeen.classnkk.AllPosts_Page$MyPagerAdapter.getCount(AllPosts_Page.java:554)
            at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:882)
            at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2905)
            at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
            at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
            at com.example.tazeen.classnkk.AllPosts_Page$MyPagerAdapter.getCount(AllPost

先谢谢

1 个答案:

答案 0 :(得分:3)

Notifydatasetchange()用于告知适配器在数据发生更改时更新其列表。你不要在getView()或getCount()中调用它。如果已从列表视图中添加或删除任何列表项,则必须调用notifyDataSetChanged()。

滚动lisview时,

getView()方法会使视图膨胀。而getCount()方法只返回ListView包含的列表项的数量。