Reputation: 29
I have this code
for (int i = 0; i < friendslocations.length(); i++)
{
JSONObject c = friendslocations.getJSONObject(i);
String friendName = c.getString(TAG_FRIENDNAME);
System.out.println("friend name " + friendName);
String friendLocation = c.getString(TAG_LOCATION);
System.out.println("friendlocation" + friendLocation);
}
where it keeps printing me the values i want.
But I need to know how to place the values that come out from this loop (the friendname and the friendloaction ) in an array list where each index contains this (friendname, friendlocation).
So how can i do this?
Upvotes: 0
Views: 43
Reputation: 6154
Wrap your two attributes in a new Object and push that object into the arraylist.
class FriendData {
public String friendName;
public String friendLocation
public FriendData(String friendName, String friendLocation) {
this.friendName=friendName;
this.friendLocation=friendLocation;
}
public String toString() {
return "friendName="+friendName+" friendLocation="+friendLocation;
}
}
List<FriendData> friendsList = new ArrayList<>();
for (int i = 0; i < friendslocations.length(); i++) {
JSONObject c = friendslocations.getJSONObject(i);
String friendName = c.getString(TAG_FRIENDNAME);
String friendLocation = c.getString(TAG_LOCATION);
friendsList.add(new FriendData(friendName, friendLocation));
}
Upvotes: 2