shivadeep
shivadeep

Reputation: 68

Arraylist keeps showing the last element n number of times

i have created an arraylist to store data which is fetched from an webservice.Data is fetched after every iteration but only the last element is displayed in arraylist.I tried various solutions but none of them seem helpful in my case

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

         }

Upvotes: 1

Views: 298

Answers (7)

AG_
AG_

Reputation: 54

ArrayList arrayList = TariffActivity.this.tariffModelsList;

place this line before for loop

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191681

You need to clear() the arraylist before the loop, otherwise you get duplicate data from onResponse shown in the list.

Only extract JSON data and add() to the list inside the loop. If you still see copies of data, see Why does my ArrayList contain N copies of the last item added to the list?

Outside the loop, you setAdapter() OR, you can call adapter.notifyDataSetChanged() assuming you already have some adapter.


Also, i <= carModelArray.length() is going to throw an exception when using getJSONObject(i).
Change to i < carModelArray.length()

Upvotes: 0

tahsinRupam
tahsinRupam

Reputation: 6405

Declare this outside the loop.

ArrayList arrayList = TariffActivity.this.tariffModelsList;

And these too outside the loop:

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

And remove the above line from the loop. Because a New ArrayList is generating and new Adapter is being set for every iteration.

Hope this helps.

Upvotes: 1

Mehul Kabaria
Mehul Kabaria

Reputation: 6622

do something like below.

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

You have to write ArrayList assignment outside of the for loop because if you write inside in loop then it reinitialization every time so element which you have to add is clear after reinitialization. That's why you have only last item in list.

Upvotes: 1

Komal12
Komal12

Reputation: 3348

Try this,

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

Upvotes: 0

Chetan Joshi
Chetan Joshi

Reputation: 5711

You have set Adapter in For Loop Also you are getting List each from your Activity while loop ruining :

Use Below Code :

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

Upvotes: 0

Ashish Ranjan
Ashish Ranjan

Reputation: 5543

move the setAdapter() out of the for loop and define the arrayList variable outside the loop.

Upvotes: 0

Related Questions