Tanveerbyn
Tanveerbyn

Reputation: 784

add object into GSON array android studio

hey guys im using android's gson library to parse my JSON. everything is working fine in that I have that selected goal array sometimes it is full of values and sometimes it is an empty array. when array is empty I want to push an object init when I try to add its index

selected_goal.add(INDEX,VALUE)

I want to push an object at the place of value please help me how can I do it…

this is my pojo:

 public class SelectedGoal {

        @SerializedName("id")
        @Expose
        private String id;
        @SerializedName("goal_name")
        @Expose
        private String goalName;
        @SerializedName("image")
        @Expose
        private String image;
        @SerializedName("position")
        @Expose
        private String position;
        @SerializedName("athlete_id")
        @Expose
        private String athleteId;
        @SerializedName("coach_id")
        @Expose
        private String coachId;
        @SerializedName("current_goal_id")
        @Expose
        private String currentGoalId;

        public String getId() {
            return id;
        }

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

        public String getGoalName() {
            return goalName;
        }

        public void setGoalName(String goalName) {
            this.goalName = goalName;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }

        public String getPosition() {
            return position;
        }

        public void setPosition(String position) {
            this.position = position;
        }

        public String getAthleteId() {
            return athleteId;
        }

        public void setAthleteId(String athleteId) {
            this.athleteId = athleteId;
        }

        public String getCoachId() {
            return coachId;
        }

        public void setCoachId(String coachId) {
            this.coachId = coachId;
        }

        public String getCurrentGoalId() {
            return currentGoalId;
        }

        public void setCurrentGoalId(String currentGoalId) {
            this.currentGoalId = currentGoalId;
        }

    }

Upvotes: 0

Views: 167

Answers (1)

Prachi Singh
Prachi Singh

Reputation: 582

selected_goal.add(INDEX,new SelectedGoal());

EDIT- To set values, make constuctor in your SelectedGoal class like.

public SelectedGoal(String id, String goalName, String image, String 
position, String athleteId, String coachId, String currentGoalId) {
    this.id = id;
    this.goalName = goalName;
    this.image = image;
    this.position = position;
    this.athleteId = athleteId;
    this.coachId = coachId;
    this.currentGoalId = currentGoalId;
}

finally add your object into list like..

 selected_goal.add(INDEX,new SelectedGoal(id,goalName,image,position,athleteId,coachId,currentGoalId));

Upvotes: 1

Related Questions