This is an extension of Android implement Parcelable objects with hashmap that contains another hashmap where I was having trouble passing 2 classes between activities using Parcelable. I'm now able to successfully pass both classes together using Parcelable. However, I'm having trouble putting the retrieved data into an array list.
Here's the EventDetails class:
public class MatchedEvent implements Parcelable {
Private String eventId;
Private String eventName;
Private Long eventUnixTime;
Private Map <String, User> attendees = new HashMap<String, User>();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(eventId);
dest.writeString(eventName);
dest.writeLong(eventUnixTime);
final int N = attendees.size();
dest.writeInt(N);
if (N > 0) {
for (Map.Entry<String, User> entry : attendees.entrySet()) {
dest.writeString(entry.getKey());
User user = entry.getValue();
dest.writeString(user.getEmail());
dest.writeString(user.getUserName());
dest.writeString(user.getUserPicture());
}
}
}
protected MatchedEvent(Parcel in) {
eventId = in.readString();
eventName = in.readString();
eventUnixTime = in.readLong();
final int N = in.readInt();
for (int i = 0; i < N; i++) {
String key = in.readString();
User user = new User();
user.email = in.readString();
user.userName = in.readString();
user.userPicture = in.readString();
attendees.put(key, user);
}
}
public Map<String, User> getAttendees() {
return attendees;
}
Here's the User class:
public class User implements Parcelable {
public String email;
public String userName;
public String userPicture;
private Boolean hasLoggedInWithPassword;
private HashMap<String, Object> dateJoined = null;
}
public User(Parcel in) {
email = in.readString();
userName = in.readString();
userPicture = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(email);
dest.writeString(userName);
dest.writeString(userPicture);
}
Here's the Fragment to retrieve the data. The EventDetails class should return a few Users. My goal is to display the name of all Users in a TextView that lists out the names with a line break. I was able to retrieve the users in an array but was not able to create an array just for the userName.
public class EventDetailsFragment extends BaseFragment {
TextView attendees;
private Map<String, User> attendeeMap;
@Override
public void onCreate(Bundle savedInstanceState) {
MatchedEvent matchedEvent = getArguments().getParcelable("eventDetailsData");
attendeesMap = matchedEvent.getAttendees();
List<User> users = new ArrayList<>();
for (String key : attendeesMap.keySet()) {
User user = attendeesMap.get(key);
users.add(user);
Log.i("Attendees Details", String.valueOf(user)); //I was able to get all users in this log.
ArrayList<String> attendeesDetails = new ArrayList<>();
attendeesDetails.add(user.getEmail());
attendeesDetails.add(user.getUserName());
attendeesDetails.add(user.getUserPicture());
for (User user1 : users) {
Log.i("Attendees Details", String.valueOf(attendeesDetails)); //?????????????????
}
}
}
The problem is where i put the mark "//?????????????????". I'm getting duplicate data. I know I shouldn't be putting a loop inside another loop. Can someone please advise how I can break this down? My goal is to put all the users' names in an array and display them in the TextView with a line break after each name.
This is what I got from the log:
I/Attendees Details: com.test.entities.User@f2b14s9
I/Attendees Details: [user1@gmail.com, Deric, https://dropbox.xxxxxx]
I/Attendees Details: com.test.entities.User@e6s26k2
I/Attendees Details: [user2@gmail.com, Jose, https://dropbox.xxxxxxxxx]
I/Attendees Details: [user2@gmail.com, Jose, https://dropbox.xxxxxxxxx]
I/Attendees Details: com.text.entities.User@b9k03l6
I/Attendees Details: [user3@gmail.com, Matt, https://dropbox.xxxxxxxxxxxxx]
I/Attendees Details: [user3@gmail.com, Matt, https://dropbox.xxxxxxxxxxxxx]
I/Attendees Details: [user3@gmail.com, Matt, https://dropbox.xxxxxxxxxxxxx]
I believe the reason why there are duplicate entries is because "attendeesDetails" is inside the loop “attendeesMap” where I got the key of each user. I’m not sure how to separate them but still able to get the data.
Thanks in advance for helping out.
-R