Home > Net >  How to store the data in the list without overwritten the data?
How to store the data in the list without overwritten the data?

Time:07-26

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.

CodePudding user response:

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