如何在PagerAdapter中调用notifydatasetchanged?

时间:2016-04-19 04:33:37

标签: android android-viewpager notifydatasetchanged android-pageradapter

我有一个列表视图,每个项目包含一个ViewPager。每次我滚动我的ViewPage时,我得到错误,说PagerAdapter改变了适配器的内容,而没有调用PagerAdapter#notifyDataSetChanged!

我也尝试在我的列表适配器和寻呼机适配器中设置notifyDataSetChanged,但它不起作用。

这是我的ListAdapter:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;

    if (convertView == null || convertView.getTag() == null) {
        convertView = inflater.inflate(R.layout.activity_detail_item, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);

    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }

    item = data.get(position);
    holder.tv_bill_nm.setText(item.getBILL_NM());
    holder.tv_use_intt_nm.setText(item.getUSE_INTT_NM());
    holder.tv_cust_nm.setText(item.getCUST_NM());
    //item.getITEM_REC() is ArrayList of object
    pageAdapter = new MyPager_Adapter(mContext, item.getITEM_REC());

    makeIndicator(holder.ll_indicator, pageAdapter.getCount(), 0);
    holder.vp_detail.setAdapter(pageAdapter);
    //holder.vp_detail.getAdapter().notifyDataSetChanged();
    holder.vp_detail.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            Log.d("page: ", position + "");
            makeIndicator(holder.ll_indicator, pageAdapter.getCount(), position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
    return convertView;
}

在每个列表项的ViewPager中,只能在页面上查看item.getITEM_REC()中的3个。

public class MyPager_Adapter extends PagerAdapter {
private List<Detail_Rec_Item> data;
private Context ctxt;
public static int num;
public static int idx;
public static int k;
public static int mod;
private boolean doNotifyDataSetChangedOnce = false;


public MyPager_Adapter(Context ctxt, List<Detail_Rec_Item> mdata) {
    this.ctxt = ctxt;
    this.data = mdata;
    k= 0;
    num = data.size(); //number of item
    idx = num / 3; //number of page
    mod = num % 3;
    if (mod != 0)
        idx++;
    if (doNotifyDataSetChangedOnce) {
        doNotifyDataSetChangedOnce = false;
        notifyDataSetChanged();
    }
}

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

@Override
public Object instantiateItem(View container, int position) {
    doNotifyDataSetChangedOnce = true;
    LayoutInflater inflater = (LayoutInflater) ctxt
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    LinearLayout view = (LinearLayout) inflater.inflate(R.layout.activity_detail_page, null);
    int max = 3;
    if (position == idx - 1)
        max = mod;

    for (int j = 0; j < max; j++) {
        LinearLayout item = (LinearLayout) inflater.inflate(R.layout.activity_detail_page_item, null);
        TextView tv_name = (TextView) item.findViewById(R.id.tv_name);
        TextView tv_amount = (TextView) item.findViewById(R.id.tv_amount);
        tv_name.setText(data.get(k).getGDS());
        tv_amount.setText(CommUtil.currencyDecrypt(data.get(k).getBILL_AMT()));
        view.addView(item);
        k++;
    }

    ((ViewPager) container).addView(view, 0);
    if (doNotifyDataSetChangedOnce) {
        doNotifyDataSetChangedOnce = false;
        notifyDataSetChanged();
    }
    return view;
}

@Override
public boolean isViewFromObject(View view, Object obj) {
    return view == ((View) obj);
}

@Override
public void destroyItem(View container, int position, Object object) {
    // TODO Auto-generated method stub
    doNotifyDataSetChangedOnce = true;
    ((ViewPager) container).removeView((View) object);
}


}

我收到如下错误:

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: 2, found: 1 Pager id: com.webcash.mobilebilling:id/vp_detail Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.webcash.WCExpensesManagementPJT_AOS.adapter.MyPager_Adapter
                                                                           at android.support.v4.view.ViewPager.populate(ViewPager.java:1000)
                                                                           at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                                           at android.support.v4.view.ViewPager$3.run(ViewPager.java:251)
                                                                           at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
                                                                           at android.view.Choreographer.doCallbacks(Choreographer.java:591)
                                                                           at android.view.Choreographer.doFrame(Choreographer.java:560)
                                                                           at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
                                                                           at android.os.Handler.handleCallback(Handler.java:730)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                           at android.os.Looper.loop(Looper.java:176)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                           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:1046)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                           at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案