Abhishek kumar
Abhishek kumar

Reputation: 4445

Add custom ArrayList data with API data using GSON in Android

I am using here GSON for Json Parsing I am getting list of Data which i am showing in Spinner , what i want to add one custom data at position first of the list.

My JSON :

[
{
    "id": 1,
    "warehouse_id": "1",
    "name": "Brookline, MA",
    "store_code": "Brookline",
    "created_at": "2016-06-15 07:08:29",
    "updated_at": null
},
{
    "id": 3,
    "warehouse_id": "3",
    "name": "New Jersey, NJ",
    "store_code": "New Jersey",
    "created_at": "2016-06-15 07:14:35",
    "updated_at": null
},
{
    "id": 4,
    "warehouse_id": "4",
    "name": "Natick, MA",
    "store_code": "Natick",
    "created_at": "2016-06-15 07:14:55",
    "updated_at": null
},
{
    "id": 6,
    "warehouse_id": "6",
    "name": "Miami, FLG",
    "store_code": "BBEV",
    "created_at": "2016-06-15 07:15:19",
    "updated_at": null
},......

Issue :

If i am adding custom data to ArrayList at first and then I parse json , it overrides the custom data & showing only data which is coming from API.

If I am parsing JSON first & then I am adding data To ArrayList then noting is showing to spinner.

In GSON there is no need to create ArrayList object:

List<DashBoardSpinner> mList;

But i have to add first row custom with API data so i have created new object of ArrayList.

List<DashBoardSpinner> mList = new ArrayList<>();

DashBoardSpinner: (POJO Class)

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class DashBoardSpinner {

    @SerializedName("id")
    @Expose
    private int id;
    @SerializedName("warehouse_id")
    @Expose
    private String warehouseId;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("store_code")
    @Expose
    private String storeCode;
    @SerializedName("created_at")
    @Expose
    private String createdAt;
    @SerializedName("updated_at")
    @Expose
    private Object updatedAt;

    public DashBoardSpinner(int id, String warehouse_id, String name, String store_code) {
        this.id = id;
        this.warehouseId = warehouse_id;
        this.name = name;
        this.storeCode = store_code;

    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getWarehouseId() {
        return warehouseId;
    }

    public void setWarehouseId(String warehouseId) {
        this.warehouseId = warehouseId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStoreCode() {
        return storeCode;
    }

    public void setStoreCode(String storeCode) {
        this.storeCode = storeCode;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public Object getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Object updatedAt) {
        this.updatedAt = updatedAt;
    }

    @Override
    public String toString() {
        return name;
    }

}

Fragment Class :

List<DashBoardSpinner> mList = new ArrayList<>();


 Gson gson = new Gson();
                            Type listType = new TypeToken<List<DashBoardSpinner>>() {
                            }.getType();
                            mList = gson.fromJson(response.toString(), listType);
                            Log.e("SIZE",""+mList.size());
                            Log.e("FirstItem",""+mList.get(0).getName());

                            //Custom Adding Data to ArrayList
                            DashBoardSpinner listFirst = new DashBoardSpinner(0,"0","All Warehouses","All Warehouses");
                            mList.add(1,listFirst);

Please tell me How can i add my own custom Data with API data using GSON. Thank you

Upvotes: 0

Views: 604

Answers (1)

Auras
Auras

Reputation: 7586

I'm not sure you're guaranteed to get an ArrayList from gson.

Try this:

                            List<DashBoardSpinner> mList = new ArrayList<>();

                            Gson gson = new Gson();
                            Type listType = new TypeToken<List<DashBoardSpinner>>() {
                            }.getType();
                            List<DashboardSpinner> apiList = gson.fromJson(response.toString(), listType);
                            Log.e("SIZE",""+apiList.size());
                            Log.e("FirstItem",""+apiList.get(0).getName());

                            //Custom Adding Data to ArrayList
                            DashBoardSpinner listFirst = new DashBoardSpinner(0,"0","All Warehouses","All Warehouses");
                            mList.add(listFirst);
                            mList.addAll(apiList);

Upvotes: 3

Related Questions