在查看Pager中无法解决notifyDataSetChanged错误?

时间:2016-03-02 11:58:27

标签: android android-viewpager notifydatasetchanged android-pageradapter

我的应用中有一个View Pager。 View寻呼机从JSON&获取Imagepath。在ImageView中显示。 View寻呼机首次使用。但是当值改变时,它会返回错误。

但我已向PagerAdapter通知了notifyDataSetChanged。

 java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 1, found: 0 Pager id: com.hello.hello:id/pager Pager class: class com.hello.hello.utils.ImageViewTouchViewPager Problematic adapter: class com.hello.hello.utils.ZoomAdapter

ASYNCTASK 1

public class FetchPromo extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            promoList = new ArrayList<String>();
            progress = new ProgressDialog(getActivity());
            progress.setMessage("Fetching Promotions from your Neighbouring store");
            progress.show();
            progress.setCanceledOnTouchOutside(false);
        }

        @Override
        protected Void doInBackground(Void... params) {
                String url = "http://46.101.126.31/mobileapp/gps/api.php?rquest=get_promotions&latitude=" + userLocation.getLatitude() + "&longitude=" + userLocation.getLongitude();
            Log.d("Path", url);
            try {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Response response = client.newCall(request).execute();
                String jsonData = response.body().string();
                try {
                    JSONObject jsonObject = new JSONObject(jsonData);
                    store_name = jsonObject.getString("store_name");
                    JSONArray promo_path = jsonObject.getJSONArray("image_path");
                    Log.d("Path", store_name);
                    for (int i = 0; i < promo_path.length(); i++) {
                        String path = promo_path.getString(i);
                        promoList.add(path);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            store.setText(store_name);
            if (promoList.isEmpty()) {
                promoList.add(placeholder);
            }
            mZoomAdapter = new ZoomAdapter(getActivity(), promoList);
            mViewPager.setAdapter(mZoomAdapter);
            mZoomAdapter.notifyDataSetChanged();
            new FetchStore().execute();
            progress.dismiss();
        }
    }

AYNCTASK 2(必须再次加载数据)

public class FetchPromoByID extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            promoList.clear();
            progress = new ProgressDialog(getActivity());
            progress.setMessage("Fetching Promotions from your Choosen store");
            progress.show();
            progress.setCanceledOnTouchOutside(false);
        }

        @Override
        protected Void doInBackground(Void... params) {
            String url = "http://46.121.116.31/mobileapp/gps/api.php?rquest=get_promotions_by_store_id&store_id=" + Choosen_id;

            Log.d("FetchPromoByID", url);
            try {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Response response = client.newCall(request).execute();
                String jsonData = response.body().string();
                try {
                    JSONObject jsonObject = new JSONObject(jsonData);
                    JSONArray promo_path = jsonObject.getJSONArray("image_path");
                    store_name = jsonObject.getString("store_name");
                    Log.d("Path", store_name);
                    for (int i = 0; i < promo_path.length(); i++) {
                        String path = promo_path.getString(i);
                        promoList.add(path);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            mZoomAdapter = new ZoomAdapter(getActivity(), promoList);
            mViewPager.setAdapter(mZoomAdapter);
            mZoomAdapter.notifyDataSetChanged();
            new FetchStore().execute();
            progress.dismiss();
        }
    }

ADAPTER

public class ZoomAdapter extends PagerAdapter {

    private Context context;
    private ArrayList<String> IMAGES = new ArrayList<>();

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

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

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

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        String url = IMAGES.get(position);
        PhotoView photoView = new PhotoView(container.getContext());

        // Now just add PhotoView to ViewPager and return it
        container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        Picasso.with(context)
                .load(url)
                .fit()
                .into(photoView);

        return photoView;
    }


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

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

FetchStore

public class FetchStore extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            storeList = new ArrayList<String>();
            storeID = new ArrayList<String>();
        }

        @Override
        protected Void doInBackground(Void... params) {

            String url = "http://46.101.116.31/mobileapp/gps/api.php?rquest=get_store";
            try {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Response response = client.newCall(request).execute();
                String jsonData = response.body().string();
                try {
                    JSONObject jsonObject = new JSONObject(jsonData);
                    JSONArray storearray = jsonObject.getJSONArray("stores");
                    for (int i = 0; i < storearray.length(); i++) {
                        JSONObject storeobj = storearray.getJSONObject(i);
                        String store = storeobj.getString("name");
                        String ID = storeobj.getString("id");
                        storeList.add(store);
                        storeID.add(ID);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            choose.setVisibility(View.VISIBLE);
        }
    }

我发现了很多类似的问题。但是没有一个解决方案适合我。请指导我。

先谢谢

1 个答案:

答案 0 :(得分:0)

我认为你的错误在这里(异步任务#2)

@Override
        protected void onPreExecute() {
            super.onPreExecute();
            promoList.clear();
            progress = new ProgressDialog(getActivity());
            progress.setMessage("Fetching Promotions from your Choosen store");
            progress.show();
            progress.setCanceledOnTouchOutside(false);
        }

你将promoList.clear()(将计数设置为0),在ZoomAdapter实例中使用而不通知。

因此请在那里通知适配器或制作临时ArrayList和clear / addAll in onPostExecute