Android将从Parcelable检索到的数据放入数组

时间:2017-04-27 22:52:45

标签: java android arrays arraylist parcelable

这是Android implement Parcelable objects with hashmap that contains another hashmap的扩展,我在使用Parcelable的活动之间传递2个类时遇到问题。 我现在能够使用Parcelable成功地将两个类一起传递。但是,我无法将检索到的数据放入数组列表中。

这是EventDetails类:

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;
}

这是User类:

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);
}

这是检索数据的片段。 EventDetails类应该返回一些用户。我的目标是在TextView中显示所有用户的名称,列出带换行符的名称。我能够检索数组中的用户,但无法为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)); //?????????????????
        }
      }


}

问题在于我把标记“// ?????????????????”。 我正在获取重复数据。我知道我不应该把循环放在另一个循环中。有人可以建议我如何打破这个?我的目标是将所有用户的名字放在一个数组中,并在每个名称后面的换行符中显示它们。

这是我从日志中得到的:

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]

我认为存在重复条目的原因是因为“attendeesDetails”位于循环“attendeesMap”中,我获得了每个用户的密钥。我不确定如何分离它们但仍然能够获取数据。

提前感谢您的帮助。

-R

2 个答案:

答案 0 :(得分:0)

这将对您有所帮助:

              @Override
              public void onCreate(Bundle savedInstanceState) {

              EventDetails eventDetails = getArguments().getParcelable("eventDetailsData");

              attendeesMap = eventDetails.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.

attendees.append(user.getEmail()+user.getUserName()+user.getUserPicture()+"\n");

              }

答案 1 :(得分:0)

我想我已经解决了这个问题。我实际上需要创建一个ArrayList<HashMap<String, String>>来存储每个用户的详细信息。然后,我将能够创建另一个数组列表来存储名称。没有更多的嵌套循环。

List<User> users = new ArrayList<>();
ArrayList<HashMap<String, String>> userArrayList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> usersDetailsHashMap = new HashMap<String, String>();
ArrayList attendeesNames = new ArrayList();

for (String key : attendeesMap.keySet()) {
    User user = attendeesMap.get(key);
    users.add(user);

    usersDetailsHashMap.put("name", user.getUserName());
    usersDetailsHashMap.put("email", user.getEmail());
    usersDetailsHashMap.put("picture", user.getUserPicture());

    userArrayList.add(usersDetailsHashMap);

    attendeesNames.add(userArrayList.get(0).get("name"));

}

Log.i("userNames", String.valueOf(attendeesNames));

这将在日志中返回:

[Deric, Jose, Matt]