Arraylist持续显示最后一个元素n次

时间:2017-03-07 06:24:55

标签: java android

我已经创建了一个arraylist来存储从webservice中获取的数据。每次迭代后都会获取数据但是只有最后一个元素显示在arraylist中。我尝试了各种解决方案,但在我的情况下它们似乎没有任何帮助

       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();
           }

         }

7 个答案:

答案 0 :(得分:1)

在循环之外声明这个。

TestBed

这些也在循环之外:

ArrayList arrayList = TariffActivity.this.tariffModelsList;

从循环中删除上面的行。因为正在生成一个新的ArrayList,并且正在为每次迭代设置新的Adapter。

希望这有帮助。

答案 1 :(得分: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);

你必须在ArrayList之外编写for loop赋值,因为如果你在循环内写,那么它每次重新初始化,因此重新初始化后你必须添加的元素是清楚的。这就是为什么你只有列表中的最后一项。

答案 2 :(得分:0)

setAdapter()移出for循环,并在循环外定义arrayList变量。

答案 3 :(得分:0)

您已在For循环中设置Adapter此外,您在List循环破坏时获得Activity

使用以下代码:

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);
            }
        }
    }

答案 4 :(得分: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);
            }

答案 5 :(得分:0)

你需要在循环前clear() arraylist,否则你会从列表中显示的onResponse中获得重复数据。

仅将JSON数据和add()提取到循环内的列表中。如果您仍然看到数据副本,请参阅Why does my ArrayList contain N copies of the last item added to the list?

外部循环,您setAdapter() ,假设您已经拥有adapter.notifyDataSetChanged(),则可以致电adapter

此外,i <= carModelArray.length()在使用getJSONObject(i)时会抛出异常 更改为i < carModelArray.length()

答案 6 :(得分:0)

ArrayList arrayList = TariffActivity.this.tariffModelsList;

将此行放在for循环

之前