How to store the data in the list without overwritten the data?

Issue

I am working on the json array loops to output the data so I can store them in the EmailAddressData list.

I have got a problem with this line under the loops:

emailAddresses = new EmailAddressData(firstname + "", email + "", url);

It will overwritten the data each time I fetch it and store it. I dont know what other ways I could use to store the data.

It will only work if I use this:

EmailAddressData[] emailAddresses = {
    new EmailAddressData("mark", "[email protected]", url),
    new EmailAddressData("chris", "[email protected]", url),
    new EmailAddressData("vik", "[email protected]", url)
};

Here is the code:

getContacts = extras.getString("getcontacts");

//loops for each array...
try {
    JSONArray jsonArray = new JSONArray(getContacts);

    if (jsonArray.length() > 0) {
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject json = jsonArray.getJSONObject(i);
            String firstname = json.getString("firstname");
            String lastname = json.getString("lastname");
            String email = json.getString("email");
            String url = json.getString("url");

            emailAddresses = new EmailAddressData(firstname + "", email + "", url);

        }
    }
} catch (JSONException e) {
  e.printStackTrace();
}

Here is the EmailAddressData class:

public class EmailAddressData {
    private String name;
    private String emailAddress;
    private String userProfileImgUrl;

    public EmailAddressData(String name, String address, String imgUrl) {
        this.name = name;
        emailAddress = address;
        userProfileImgUrl = imgUrl;
    }

    public String getName() {

        return name;
    }

    public String getEmail() {

        return emailAddress;
    }

    public String getUserProfileImgUrl() {

        return userProfileImgUrl;
    }
}

Could you please show me an example of what is the proper way I could use to store the data into the EmailAddressData list?

Thank you.

Solution

If I understand the problem correctly. Declare a list and store all objects inside it

getContacts = extras.getString("getcontacts");

List<EmailAddressData> list = new ArrayList<>();

//loops for each array...
try {
    JSONArray jsonArray = new JSONArray(getContacts);

    if (jsonArray.length() > 0) {
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject json = jsonArray.getJSONObject(i);
            String firstname = json.getString("firstname");
            String lastname = json.getString("lastname");
            String email = json.getString("email");
            String url = json.getString("url");

            emailAddresses = new EmailAddressData(firstname + " " + lastname, email, url);
            list.add(emailAddresses);

        }
    }
} catch (JSONException e) {
  e.printStackTrace();
}

Edit: if you want to store them in an array, use this:

getContacts = extras.getString("getcontacts");

//loops for each array...
try {
    JSONArray jsonArray = new JSONArray(getContacts);
     
    EmailAddressData[] array = new EmailAddressData[jsonArray.length()];

    if (jsonArray.length() > 0) {
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject json = jsonArray.getJSONObject(i);
            String firstname = json.getString("firstname");
            String lastname = json.getString("lastname");
            String email = json.getString("email");
            String url = json.getString("url");

            array[i] = new EmailAddressData(firstname + " " + lastname, email, url);

        }
    }
} catch (JSONException e) {
  e.printStackTrace();
}

Answered By – Abdo21

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published