1

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

         }
shivadeep
  • 68
  • 1
  • 11
  • Welcome to Stack Overflow! It looks like you need to learn to use a debugger. Please help yourself to some [complementary debugging techniques](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you still have issues afterwards, please feel free to come back with more details. – Joe C Mar 07 '17 at 06:27
  • If you used Retrofit + Gson, you wouldn't need to manually parse your JSON – OneCricketeer Mar 07 '17 at 06:29
  • @shivadeep, TrafficAdapter is set at incorrect place, since it is inside the loop - value gets overridden and so the last value of the arraylist is set to it. So you have to assign the arraylist to the Traffic adapter after the loop ends, Please check my answer below – Srikanth Balaji Mar 07 '17 at 06:33
  • If I see this sort of problem my first question is: does `TariffModel` have only static fields? Because then no matter how many `TariffModel` objects you create all of them will have the same properties – Thomas Kläger Mar 07 '17 at 06:52
  • no it doesn't contain – shivadeep Mar 07 '17 at 06:56
  • If none of the answers help I would still recommend that you add the definition of your `TariffModel` class (at least the field declarations and the constructor) – Thomas Kläger Mar 07 '17 at 11:19

7 Answers7

1

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.

tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
1

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.

Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
  • Welcome to Stack Overflow! While you may have solved this user's problem, code-only answers are not very helpful to users who come to this question in the future. Please edit your answer to explain why your code solves the original problem. – Joe C Mar 07 '17 at 06:30
  • @JoeC: I have added the explanation. – Mehul Kabaria Mar 07 '17 at 06:36
0

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

Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
0

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);
            }
        }
    }
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

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);
            }
Komal12
  • 3,340
  • 4
  • 16
  • 25
  • @shivadeep set your adapter after for loop – Komal12 Mar 07 '17 at 06:33
  • Welcome to Stack Overflow! While you may have solved this user's problem, code-only answers are not very helpful to users who come to this question in the future. Please edit your answer to explain why your code solves the original problem – Srikanth Balaji Mar 07 '17 at 06:35
0

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

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Like I said, you need to clear before the loop. As the very first thing in `onResponse`. If you only see one item, then your JSON array could only have one item. – OneCricketeer Mar 07 '17 at 07:27
  • You mean `e.printStackTrace();`? Well, sure, you have an error. Please debug your app with a [mcve] – OneCricketeer Mar 07 '17 at 07:35
0

ArrayList arrayList = TariffActivity.this.tariffModelsList;

place this line before for loop

AG_
  • 54
  • 3
  • Place these two lines after for and try : TariffActivity.this.tariffAdapter = new TariffAdapter(TariffActivity.this.mContext,arrayList); TariffActivity.this.mRecyclerView.setAdapter(tariffAdapter); – AG_ Mar 08 '17 at 12:29
  • i <= carModelArray.length() change this to i < carModelArray.length() – AG_ Mar 08 '17 at 12:43