应用程序的PagerAdapter在不调用notifyDataSetChanged()的情况下更改了适配器的内容

时间:2017-04-15 15:49:59

标签: java android android-viewpager notifydatasetchanged

我正在开发一款关于android的简单游戏。我有一个从服务器获取数据并将其加载到ViewPager的活动。这是活动代码段:

04-15 20:14:01.894 29812-29812/com.s3.smartswitch  D/ViewRootImpl@f99395d[HomeActivity]: ViewPostImeInputStage processPointer 0
04-15 20:14:01.992 29812-29812/com.s3.smartswitch D/ViewRootImpl@f99395d[HomeActivity]: ViewPostImeInputStage processPointer 1
04-15 20:14:02.034 29812-29812/com.s3.smartswitch D/ViewRootImpl@f99395d[HomeActivity]: MSG_WINDOW_FOCUS_CHANGED 0
04-15 20:14:02.151 29812-29812/com.s3.smartswitch D/EF-BTBee: >>Start Write data
04-15 20:14:02.152 29812-29812/com.s3.smartswitch D/EF-BTBee: >>Start Write data
04-15 20:14:02.153 29812-29812/com.s3.smartswitch D/EF-BTBee: >>End Write data
04-15 20:14:02.170 29812-29812/com.s3.smartswitch D/InputTransport: Input channel constructed: fd=82
04-15 20:14:02.171 29812-29812/com.s3.smartswitch D/ViewRootImpl@4841f01[ClientSocketActivity]: setView = DecorView@979b1a6[ClientSocketActivity] touchMode=true
04-15 20:14:02.172 29812-29812/com.s3.smartswitch D/EF-BTBee: onServiceConnected mBound
04-15 20:14:02.205 29812-29909/com.s3.smartswitch D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1080x1920]-format:1
04-15 20:14:02.206 29812-29812/com.s3.smartswitch D/ViewRootImpl@4841f01[ClientSocketActivity]: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
04-15 20:14:02.206 29812-29812/com.s3.smartswitch D/ViewRootImpl@4841f01[ClientSocketActivity]: MSG_WINDOW_FOCUS_CHANGED 1
04-15 20:14:02.207 29812-29812/com.s3.smartswitch I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
04-15 20:14:02.220 29812-29812/com.s3.smartswitch D/InputTransport: Input channel constructed: fd=88
04-15 20:14:02.220 29812-29812/com.s3.smartswitch D/InputTransport: Input channel destroyed: fd=86

这是我的PagerAdapter:

    url = new ArrayList<>();
    name = new ArrayList<>();
    viewPager = (ViewPager) findViewById(R.id.viewPager);
    viewPagerAdapter = new ViewPagerAdapter(getApplicationContext(),url,name);
    viewPager.setAdapter(viewPagerAdapter);

    AppController.socket.emit("getDoodle","")
            .on("doodleList", new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    JSONArray data = (JSONArray)args[0];
                    Log.e("MYTAG",data.toString());
                    url.clear();
                    name.clear();
                    for(int i=0;i<AppController.playersId.size();i++){
                        try {
                            if(!data.getJSONObject(i).getString("doodle").equals("null")) {
                                url.add(data.getJSONObject(i).getString("doodle"));
                                name.add(data.getJSONObject(i).getString("id"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }


                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            viewPagerAdapter.notifyDataSetChanged;
                        }
                    });
                }
            });

我的错误日志:

    class ViewPagerAdapter extends PagerAdapter {

Context mContext;
ArrayList<String> mUri,mId;

public ViewPagerAdapter(Context context, ArrayList<String> imageUri, ArrayList<String> sid){
    mContext = context;
    mUri = imageUri;
    this.mId=sid;
    Log.e("MYTAG",mUri.toString());
}
@Override
public int getCount() {
    return mUri.size();
}

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

@Override
public Object instantiateItem(ViewGroup container, final int position) {
    LayoutInflater mLayoutInflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = mLayoutInflater.inflate(R.layout.pager_child, container, false);

    final ImageView imageView = (ImageView) itemView.findViewById(R.id.doodleView);
    TextView nameText = (TextView) itemView.findViewById(R.id.ownerText);
    String temp = mUri.get(position);
    if(!temp.equals("")){
        Log.e("replace","2");
        Glide.with(mContext)
                .load(temp)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(imageView);
        nameText.setText(mId.get(position));
    }
    Log.d("pager",mUri.get(position));
    itemView.setTag(position);
    container.addView(itemView);
    return itemView;
}

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

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

我不知道我做错了什么..感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

在添加项目期间无法刷新ViewPager时出现问题。如果更改的计数与保存的计数不相等,您将收到此错误。

解决它:

更改适配器项后,应调用viewPagerAdapter.notifyDataSetChanged; ..

代码:

 public ViewPagerAdapter(Context context, ArrayList<String> imageUri, ArrayList<String> sid){
    mContext = context;
    mUri = imageUri;
    this.mId=sid;
    notifyDataSetChanged();   //add here
    Log.e("MYTAG",mUri.toString());
}

修改

致电viewPagerAdapter.notifyDataSetChanged(); //您缺少括号