我有一个for循环,并且将值添加到HashMap for循环中。基本上我有一个帖子,它有评论。一个职位有8点意见,我需要我的HashMap的内容看,如下图所示:如何在Android中的for循环的每次迭代中为HashMap添加值
PostId1, comment1
PostId1, comment2
PostId1, comment3
PostId1, comment4
PostId1, comment5
PostId1, comment6
PostId1, comment7
PostId1, comment8
但是,现在我的HashMap中只包含第一个值。只有在第一次迭代中,值才会被添加到HashMap中,我如何在所有8次迭代中为hashmap添加值。
PS:所有八个值的PostID应该相同。
我当前的代码:
for (int i2 = 0; i2 < conversationArray.length(); i2++) {
JSONObject conversationArray1 = conversationArray.getJSONObject(i2);
contentConversation = conversationArray1.getString("content");
commenterId = conversationArray1.getString("commenterId");
commenterName = conversationArray1.getString("commenterName");
commenterPhotos = conversationArray1.getString("commenterPhotos");
postIdForComments = conversationArray1.getString("postId");
lastDateUpdatedConversation = conversationArray1.getString("lastDateUpdated");
dateCreatedConversation = conversationArray1.getString("dateCreated");
commentDescription.add(contentConversation);
commentUserName.add(commenterName);
commentProfileImageLink.add(commenterPhotos);
commentProfileImageHashMap.put(postIdForComments, commenterPhotos);
commentDescriptionHashMap.put(postIdForComments, contentConversation);
commentUserNameHashMap.put(postIdForComments, commenterName);
}
请让我知道什么样的变化,我应该让我的代码,实现我的目标。所有建议都欢迎。
你似乎不清楚包含HashMap是如何工作 - 项对的,关键的第一个项目,**必须* *是唯一的,否则你不应该使用HashMap。可能你想使用的是'Map>',它被实现为'HashMap >'。 –
@HovercraftFullOfEels好的。我会尽力实现这一点。 – Kiran