无法在android中正确添加值到HashMap

时间:2016-01-02 10:48:19

标签: java android arraylist hashmap

我在代码中使用Hashmap<String, ArrayList<String>>。我有不同的帖子。 post1有8条评论,post2有3条评论,post3有4条评论。

我正在向hashmap添加值,如下所示:

HashMap.put(postId, commentList);

我正在通过for循环阅读每篇文章的所有评论。总共有5个帖子。我得到大小为5的HashMap。但我的问题是所有hashmap值的arraylist大小都相同。相反,HashMap的第一个值的arraylist值应为8,第二个hashmap的arraylist值应为3等。

请让我知道我在做什么错误。我的代码如下所示:

 JSONArray conversationArray = c.getJSONArray("conversation");

for (int i2 = 0; i2 < conversationArray.length(); i2++) {

JSONObject conversationArray1 = conversationArray.getJSONObject(i2);
contentConversation = conversationArray1.getString("content");
commenterName = conversationArray1.getString("commenterName");
postIdForComments = conversationArray1.getString("postId");

commentDescription.add(contentConversation);
commentUserName.add(commenterName);

commentDescriptionHashMap.put(postIdForComments, commentDescription);
commentUserNameHashMap.put(postIdForComments, commentUserName);

     }

我的Json对象:

"conversation": [
        {
          "_id": "568525b97113a4b417db2eb0",
          "content": "kl;,",
          "commenterId": "56332edfad441746cbd15000",
          "commenterName": "yashas cl",
          "commenterPhotos": "https://toadfiles.s3.amazonaws.com/56332edfad441746cbd15000_1450158467841images.jpg",
          "postId": "568520a01931912c03eb1a16",
          "lastDateUpdated": "2015-12-31T12:55:21.151Z",
          "dateCreated": "2015-12-31T12:55:21.151Z",
          "isDeleted": false,
          "__v": 0,
          "against": [],
          "for": [],
          "commenterUserName": [
            "yashas"
          ]
        }

我在完成for循环后尝试清除Arraylists。当我清除arraylists时,hashmap列表的值将变为零。我正在努力弄清楚已经发生了什么。请给我关于这个问题的建议。

2 个答案:

答案 0 :(得分:0)

试试这个,

创建一个Comment

class Comment
{
  String commentId,content,commentorId,commenterName,postId; //You can add more, depends on your requirement
}

Set<String> postIdSet;
JSONArray conversationArray = c.getJSONArray("conversation");

for (int j = 0; j < conversationArray.length(); j++) {

JSONObject comment = conversationArray.getJSONObject(j);
String postIdForComments = comment.getString("postId");
postIdSet = new HashSet<String>();
postIdSet.add(postIdForComments);

     }
for (int j = 0; j < conversationArray.length(); j++) {
      List<Comment> commentList = new ArrayList<Comment>();
      Comment comment = new Comment();
      JSONObject comment = conversationArray.getJSONObject(j);
      comment.commentId = comment.getString("_id");
      comment.content = comment.getString("content"); 
      comment.commentorId = comment.getString("commentorId");
      comment.commentorName = comment.getString("commentorName");
      comment.postId = comment.getString("postId");
      commentList.add(comment);
 }

   ArrayList<HashMap<String,ArrayList<Comment>>> arrayListOfHashmap = new ArrayList<HashMap<String,ArrayList<Comment>>>();
  for(j=0;j<postIdSet.size();j++)
  {
   ArrayList<Comment> cList;
   for(i =0 ;i<commentList.size();i++)
   {
      if(postIdSet.get(i).equals(commentList.get(i).postId))
      {
         cList = new ArrayList<Comment>();
         cList.add(commentList.get(i));    

      }
   }
    HashMap<String,ArrayList<Comment>> tempMap = new HashMap<String,ArrayList<Comment>>();
    tempMap.put(postIdSet.get(i),cList);
    arrayListOfHashmap.add(temp);  
  }

  Log.e("Map",arrayListOfHashmap);

由于我没有在编辑器中输入错误,可能会有很多错别字,但您应该可以修复它。

答案 1 :(得分:0)

我想出了这个问题。我应该在每次更新hashmap之前初始化arraylist。在第二次添加值之前,我应该将arraylist初始化为

ArrayList<String> = new ArrayList<String>. 

我这样做只有一次。这就是问题所在。