0

我在我的應用程序中有一個View Pager。 View分頁器從ImageView中顯示JSON &中的Imagepath。 View尋呼機第一次工作。但是當這些值改變時,它會返回一個錯誤。在視圖尋呼機中無法解決notifyDataSetChanged錯誤?

但我已通知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); 
     } 
    } 

我發現很多這樣類似的問題。但沒有任何解決方案爲我工作。請指導我。

由於提前

+0

郵編()。 –

+0

@JagjitSingh張貼。 – user3467240

+0

嘗試通過設置setOffscreenPageLimit mViewPager.setOffscreenPageLimit(promoList.size()); –

回答

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情況下使用無需通知。

所以有通知適配器或作出FetchStore臨時ArrayList和clear/addAll in onPostExecute

+0

它不起作用! – user3467240

+0

請發佈更新的代碼 – aelimill

相關問題