2017-03-07 98 views
0

我創建了一個ArrayList來存儲從webservice獲取的數據。每次迭代後都會獲取數據,但只有最後一個元素顯示在arraylist中。我嘗試了各種解決方案,但沒有在我的情況下似乎很有幫助Arraylist不斷顯示最後一個元素n次數

 public void onResponse(JSONObject paramAnonymousJSONObject) 
     { 
    JSONObject localJSONObject; 
    int j; 
    try 
    { 
     localJSONObject = paramAnonymousJSONObject.getJSONObject("status"); 
     j = localJSONObject.getInt("code"); 
     if (j == 200) { 
     JSONArray carModelArray = paramAnonymousJSONObject.getJSONArray("carModels"); 
     for (int i = 0; i <= carModelArray.length(); i++) { 
      JSONObject tariffResponse = null; 
      JSONObject tariff = carModelArray.getJSONObject(i); 
      try { 
      carId = tariff.getString("id"); 
      segment = tariff.getString("segment"); 
      imageURL = tariff.getString("imageURL"); 
      tariffResponse = tariff.getJSONObject("tariffResponse"); 
      CarType = tariffResponse.getString("carModel"); 
      weekdayTariff = tariffResponse.getString("weekdayTariff"); 
      weekendTariff = tariffResponse.getString("weekendTariff"); 
      peakSeasonTariff = tariffResponse.getString("peakSeasonTariff"); 
      maintenanceCharge = tariffResponse.getString("maintainanceFee"); 
      securityDeposite = tariffResponse.getString("securityDeposite"); 
      ArrayList arrayList = TariffActivity.this.tariffModelsList; 
      arrayList.add(new TariffModel(carId, CarType, imageURL, "\u20b9 " + weekdayTariff, "\u20b9 " + weekendTariff, "\u20b9 " + peakSeasonTariff, "\u20b9 " + securityDeposite, segment, "\u20b9 " + maintenanceCharge)); 
      TariffActivity.this.tariffAdapter = new TariffAdapter(TariffActivity.this.mContext,arrayList); 
      TariffActivity.this.mRecyclerView.setAdapter(tariffAdapter); 
      } catch (JSONException e) { 
      e.printStackTrace(); 
      } 

     } 
+0

歡迎使用堆棧溢出!它看起來像你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然有問題,請隨時返回更多詳情。 –

+0

如果你使用Retrofit + Gson,你不需要手動解析你的JSON –

+0

@shivadeep,TrafficAdapter被設置在不正確的地方,因爲它在循環內部 - 值被覆蓋,因此arraylist的最後一個值被設置爲它。 所以你必須在循環結束後將數組列表分配給交通適配器,請在 –

回答

1

在循環之外聲明它。

ArrayList arrayList = TariffActivity.this.tariffModelsList; 

而這些太外循環:

TariffActivity.this.tariffAdapter = new TariffAdapter(TariffActivity.this.mContext,arrayList); 
TariffActivity.this.mRecyclerView.setAdapter(tariffAdapter); 

並從環上面的行。因爲新的ArrayList正在生成,並且爲每次迭代都設置了新的Adapter。

希望這會有所幫助。

+0

它沒有工作 – shivadeep

+0

重新分配似乎毫無意義 –

+0

@shivadeep您是否刪除了ArrayList arrayList = TariffActivity.this.tariffModelsList;這從循環內部? – tahsinRupam

1

做下面的事情。

public void onResponse(JSONObject paramAnonymousJSONObject) 
    { 
     JSONObject localJSONObject; 
     int j; 
     try 
     { 
      localJSONObject = paramAnonymousJSONObject.getJSONObject("status"); 
      j = localJSONObject.getInt("code"); 
      if (j == 200) { 
       JSONArray carModelArray = paramAnonymousJSONObject.getJSONArray("carModels"); 
       ArrayList arrayList = TariffActivity.this.tariffModelsList; 
       for (int i = 0; i <= carModelArray.length(); i++) { 
        JSONObject tariffResponse = null; 
        JSONObject tariff = carModelArray.getJSONObject(i); 
        try { 
         carId = tariff.getString("id"); 
         segment = tariff.getString("segment"); 
         imageURL = tariff.getString("imageURL"); 
         tariffResponse = tariff.getJSONObject("tariffResponse"); 
         CarType = tariffResponse.getString("carModel"); 
         weekdayTariff = tariffResponse.getString("weekdayTariff"); 
         weekendTariff = tariffResponse.getString("weekendTariff"); 
         peakSeasonTariff = tariffResponse.getString("peakSeasonTariff"); 
         maintenanceCharge = tariffResponse.getString("maintainanceFee"); 
         securityDeposite = tariffResponse.getString("securityDeposite"); 

         arrayList.add(new TariffModel(carId, CarType, imageURL, "\u20b9 " + weekdayTariff, "\u20b9 " + weekendTariff, "\u20b9 " + peakSeasonTariff, "\u20b9 " + securityDeposite, segment, "\u20b9 " + maintenanceCharge)); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 
       TariffActivity.this.tariffAdapter = new TariffAdapter(TariffActivity.this.mContext,arrayList); 
       TariffActivity.this.mRecyclerView.setAdapter(tariffAdapter); 

你必須寫for loop之外ArrayList分配,因爲如果你在寫循環裏面,然後重新初始化它每次都這樣,你必須添加元素是重新初始化以後清晰。這就是爲什麼你只有列表中的最後一個項目。

+0

歡迎來到Stack Overflow!儘管您可能已經解決了此用戶的問題,但僅有代碼的答案對於未來出現此問題的用戶來說並不是很有幫助。請編輯您的答案,以解釋爲什麼您的代碼可以解決原始問題。 –

+0

@JoeC:我添加了解釋。 –

0

setAdapter()移出for循環,並在循環外定義arrayList變量。

+0

確實沒有起作用 – shivadeep

0

你在For循環設置Adapter您也越來越每個ListActivity while循環搞壞:

使用下面的代碼:

public void onResponse(JSONObject paramAnonymousJSONObject) { 
     JSONObject localJSONObject; 
     int j; 
     try { 
      localJSONObject = paramAnonymousJSONObject.getJSONObject("status"); 
      j = localJSONObject.getInt("code"); 
      if (j == 200) { 
       JSONArray carModelArray = paramAnonymousJSONObject.getJSONArray("carModels"); 
       ArrayList arrayList = TariffActivity.this.tariffModelsList; 
       for (int i = 0; i <= carModelArray.length(); i++) { 
        JSONObject tariffResponse = null; 
        JSONObject tariff = carModelArray.getJSONObject(i); 
        try { 
         carId = tariff.getString("id"); 
         segment = tariff.getString("segment"); 
         imageURL = tariff.getString("imageURL"); 
         tariffResponse = tariff.getJSONObject("tariffResponse"); 
         CarType = tariffResponse.getString("carModel"); 
         weekdayTariff = tariffResponse.getString("weekdayTariff"); 
         weekendTariff = tariffResponse.getString("weekendTariff"); 
         peakSeasonTariff = tariffResponse.getString("peakSeasonTariff"); 
         maintenanceCharge = tariffResponse.getString("maintainanceFee"); 
         securityDeposite = tariffResponse.getString("securityDeposite"); 

         arrayList.add(new TariffModel(carId, CarType, imageURL, "\u20b9 " + weekdayTariff, "\u20b9 " + weekendTariff, "\u20b9 " + peakSeasonTariff, "\u20b9 " + securityDeposite, segment, "\u20b9 " + maintenanceCharge)); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 
       TariffActivity.this.tariffAdapter = new TariffAdapter(TariffActivity.this.mContext, arrayList); 
       TariffActivity.this.mRecyclerView.setAdapter(tariffAdapter); 
      } 
     } 
    } 
0

試試這個,

public void onResponse(JSONObject paramAnonymousJSONObject) 
      { 
     JSONObject localJSONObject; 
     int j; 
      ArrayList arrayList = TariffActivity.this.tariffModelsList; 

     try 
     { 
      localJSONObject = paramAnonymousJSONObject.getJSONObject("status"); 
      j = localJSONObject.getInt("code"); 
      if (j == 200) { 
      JSONArray carModelArray = paramAnonymousJSONObject.getJSONArray("carModels"); 
      for (int i = 0; i <= carModelArray.length(); i++) { 
       JSONObject tariffResponse = null; 
       JSONObject tariff = carModelArray.getJSONObject(i); 
       try { 
       carId = tariff.getString("id"); 
       segment = tariff.getString("segment"); 
       imageURL = tariff.getString("imageURL"); 
       tariffResponse = tariff.getJSONObject("tariffResponse"); 
       CarType = tariffResponse.getString("carModel"); 
       weekdayTariff = tariffResponse.getString("weekdayTariff"); 
       weekendTariff = tariffResponse.getString("weekendTariff"); 
       peakSeasonTariff = tariffResponse.getString("peakSeasonTariff"); 
       maintenanceCharge = tariffResponse.getString("maintainanceFee"); 
       securityDeposite = tariffResponse.getString("securityDeposite"); 

       arrayList.add(new TariffModel(carId, CarType, imageURL, "\u20b9 " + weekdayTariff, "\u20b9 " + weekendTariff, "\u20b9 " + peakSeasonTariff, "\u20b9 " + securityDeposite, segment, "\u20b9 " + maintenanceCharge)); 

       } catch (JSONException e) { 
       e.printStackTrace(); 
       } 
      } 
      } 
     } 
     catch (Exception e) { 
       e.printStackTrace(); 
       }  

       TariffActivity.this.tariffAdapter = new TariffAdapter(TariffActivity.this.mContext,arrayList); 
       TariffActivity.this.mRecyclerView.setAdapter(tariffAdapter); 
      } 
+0

@shivadeep在for循環後設置你的適配器 – user2025187

+0

歡迎來到Stack Overflow!儘管您可能已經解決了此用戶的問題,但僅有代碼的答案對於未來出現此問題的用戶來說並不是很有幫助。請編輯你的答案,以解釋爲什麼你的代碼解決了原始問題 –

0

你在循環之前需要clear() arraylist,否則你會從得到重複的數據列表中顯示的。

只提取JSON數據和add()到循環內的列表。如果你仍然可以看到數據的副本,請參見Why does my ArrayList contain N copies of the last item added to the list?

循環,你setAdapter()OR,您可以撥打adapter.notifyDataSetChanged()假設你已經有了一些adapter


此外,i <= carModelArray.length()會使用getJSONObject(i)時拋出異常。
更改爲i < carModelArray.length()

+0

如果我使用clear()只有一個副本仍然 – shivadeep

+0

就像我說的,你需要在循環之前清除。作爲「onResponse」中的第一件事。如果你只看到一個項目,那麼你的JSON數組只能有一個項目。 –

+0

它去錯誤響應 – shivadeep

0

ArrayList arrayList = TariffActivity.this.tariffModelsList;

將此行放在循環之前

+0

調試您的應用程序,它是否無效 – shivadeep

+0

將這兩行放在for之後並嘗試:TariffActivity.this.tariffAdapter = new TariffAdapter(TariffActivity.this.mContext,arrayList); TariffActivity.this.mRecyclerView.setAdapter(tariffAdapter); –

+0

它沒有工作 – shivadeep