当前位置: 代码迷 >> java >> 在HashMap中遇到问题 >,(在Android中创建评论视图)
  详细解决方案

在HashMap中遇到问题 >,(在Android中创建评论视图)

热度:51   发布时间:2023-07-25 19:30:49.0

收到此Json回复

{
"result": "success",
"data": [
    {
        "comment": "hi",
        "addedon": "2019-03-01 09:31:28",
        "user_id": "1",
        "id": "22",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "hixdh ",
        "addedon": "2019-03-01 09:31:32",
        "user_id": "1",
        "id": "23",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "xy uf",
        "addedon": "2019-03-01 09:31:36",
        "user_id": "1",
        "id": "24",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "cgui ",
        "addedon": "2019-03-01 09:31:39",
        "user_id": "1",
        "id": "25",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "hi",
        "addedon": "2019-03-01 09:33:32",
        "user_id": "1",
        "id": "26",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "fg ",
        "addedon": "2019-03-01 09:33:34",
        "user_id": "1",
        "id": "27",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "chkk hhh",
        "addedon": "2019-03-01 09:33:39",
        "user_id": "1",
        "id": "28",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "yubvcg ",
        "addedon": "2019-03-01 09:33:43",
        "user_id": "1",
        "id": "29",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "ggi ",
        "addedon": "2019-03-01 09:33:47",
        "user_id": "1",
        "id": "30",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "cfg hj",
        "addedon": "2019-03-01 09:33:50",
        "user_id": "1",
        "id": "31",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "vbj",
        "addedon": "2019-03-01 09:33:53",
        "user_id": "1",
        "id": "32",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "hehh",
        "addedon": "2019-03-01 11:32:13",
        "user_id": "18",
        "id": "33",
        "name": "Test User 3",
        "photo": "avatar10.png"
    },
    {
        "comment": "hhh",
        "addedon": "2019-03-01 11:43:26",
        "user_id": "18",
        "id": "37",
        "name": "Test User 3",
        "photo": "avatar10.png"
    },
    {
        "comment": "hahahah",
        "addedon": "2019-03-01 12:04:14",
        "user_id": "18",
        "id": "42",
        "name": "Test User 3",
        "photo": "avatar10.png"
    },
    {
        "comment": "hii",
        "addedon": "2019-03-01 12:16:05",
        "user_id": "18",
        "id": "43",
        "name": "Test User 3",
        "photo": "avatar10.png"
    },
    {
        "comment": "hou",
        "addedon": "2019-03-01 12:16:12",
        "user_id": "18",
        "id": "44",
        "name": "Test User 3",
        "photo": "avatar10.png"
    },
    {
        "comment": "hi",
        "addedon": "2019-03-01 12:16:28",
        "user_id": "18",
        "id": "45",
        "name": "Test User 3",
        "photo": "avatar10.png"
    },
    {
        "comment": "hi",
        "addedon": "2019-03-01 12:16:45",
        "user_id": "18",
        "id": "46",
        "name": "Test User 3",
        "photo": "avatar10.png"
    },
    {
        "comment": "hi",
        "addedon": "2019-03-01 12:16:52",
        "user_id": "18",
        "id": "47",
        "name": "Test User 3",
        "photo": "avatar10.png"
    },
    {
        "comment": "gg",
        "addedon": "2019-03-01 12:17:09",
        "user_id": "18",
        "id": "48",
        "name": "Test User 3",
        "photo": "avatar10.png"
    },
    {
        "comment": "heyy",
        "addedon": "2019-03-01 12:31:26",
        "user_id": "1",
        "id": "49",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "fff",
        "addedon": "2019-03-01 12:36:52",
        "user_id": "1",
        "id": "52",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "hhh",
        "addedon": "2019-03-02 00:13:41",
        "user_id": "18",
        "id": "54",
        "name": "Test User 3",
        "photo": "avatar10.png"
    },
    {
        "comment": "jii",
        "addedon": "2019-03-02 02:33:55",
        "user_id": "1",
        "id": "61",
        "name": "chitransh",
        "photo": "user.png"
    },
    {
        "comment": "dsdsdsdsd",
        "addedon": "2019-03-03 03:44:33",
        "user_id": "1",
        "id": "63",
        "name": "chitransh",
        "photo": "user.png"
    }
],
"reply": {
    "22": [
        {
            "comment": "hahaha",
            "addedon": "2019-03-02 00:34:50",
            "user_id": "18",
            "id": "58",
            "name": "Test User 3",
            "photo": "avatar10.png"
        },
        {
            "comment": "@chitransh heyyy",
            "addedon": "2019-03-03 03:44:59",
            "user_id": "1",
            "id": "65",
            "name": "chitransh",
            "photo": "user.png"
        },
        {
            "comment": "@chitransh heyy test user",
            "addedon": "2019-03-03 03:45:23",
            "user_id": "1",
            "id": "66",
            "name": "chitransh",
            "photo": "user.png"
        },
        {
            "comment": "@chitransh hiw are you",
            "addedon": "2019-03-03 04:06:09",
            "user_id": "1",
            "id": "67",
            "name": "chitransh",
            "photo": "user.png"
        }
    ],
    "23": [
        {
            "comment": "@chitransh hhhh",
            "addedon": "2019-03-02 01:04:58",
            "user_id": "18",
            "id": "59",
            "name": "Test User 3",
            "photo": "avatar10.png"
        }
    ],
    "24": [],
    "25": [],
    "26": [],
    "27": [],
    "28": [],
    "29": [],
    "30": [],
    "31": [
        {
            "comment": "@chitranshdsdsdsdsd",
            "addedon": "2019-03-02 05:07:44",
            "user_id": "1",
            "id": "62",
            "name": "chitransh",
            "photo": "user.png"
        }
    ],
    "32": [],
    "33": [],
    "37": [],
    "42": [],
    "43": [],
    "44": [],
    "45": [],
    "46": [],
    "47": [],
    "48": [],
    "49": [
        {
            "comment": "chitransh hi",
            "addedon": "2019-03-01 12:32:47",
            "user_id": "1",
            "id": "50",
            "name": "chitransh",
            "photo": "user.png"
        },
        {
            "comment": "chitransh sdff",
            "addedon": "2019-03-01 12:33:00",
            "user_id": "1",
            "id": "51",
            "name": "chitransh",
            "photo": "user.png"
        },
        {
            "comment": "chitransh hi",
            "addedon": "2019-03-01 12:42:59",
            "user_id": "1",
            "id": "53",
            "name": "chitransh",
            "photo": "user.png"
        }
    ],
    "52": [],
    "54": [
        {
            "comment": "@Test User 3kjj",
            "addedon": "2019-03-02 01:20:55",
            "user_id": "18",
            "id": "60",
            "name": "Test User 3",
            "photo": "avatar10.png"
        }
    ],
    "61": [
        {
            "comment": "@chitranshdsdsdsdsdsd",
            "addedon": "2019-03-03 03:44:40",
            "user_id": "1",
            "id": "64",
            "name": "chitransh",
            "photo": "user.png"
        }
    ],
    "63": []
}

}

完成数据JSONArray,现在可以处理答复了,正如您在回复中看到的JSONArray,我将从数据中获取ID,因此我将该ID存储为Map中的key ex。

 Map<String,Arraylist<ReplyCommentModel>>

所以String就是那个id。

现在在RecyclerView项目中的buttonOnClick上,我正在使用此代码->

 viewRepliesButton.setOnClickListener(v -> {
        if (collpased){
            collpased = false;
            replyRecyclerView.setVisibility(View.VISIBLE);
            GroupAdapter<ViewHolder> adapter = new GroupAdapter<>();
            replyRecyclerView.setAdapter(adapter);
            replyRecyclerView.setLayoutManager(new LinearLayoutManager(context));
            replyCommentModels = replyModelsMap.get(String.valueOf(model.comment_id));
            if(replyCommentModels != null){
                Log.e("replComments ","Size" +replyCommentModels.size());
                for(ReplyCommentModel replyCommentModel : replyCommentModels){
                    adapter.add(new ReplyCommentItem(replyCommentModel,context));
                }
            }
            viewRepliesButton.setText("hide");
        } else {
            collpased = true;
            replyRecyclerView.setVisibility(View.GONE);
            viewRepliesButton.setText("Tap to view reply");

        }
    });

但是地图中的所有项目都是可见的。在recyclerView中,我的代码中是否存在某些问题? 由于我从适配器项模型传递的注释id的值是JSON响应中的Data键,因此您现在可以在上方看到,我在列表上单击并将其id传递给map以获取该特定id的数组列表,也许map是曾经这样做过,还是有其他一些更好的方式做到这一点? Map <>与Arraylist tbh一起使用时的新功能,任何适当的指导将不胜感激

我这样保存我的地图

private Map<String,ArrayList<ReplyCommentModel>> replyModelMap = new HashMap<>();
 try {
            JSONObject obj = new JSONObject(response);

            String status = obj.getString("result");
            if (status.equals("success")) {
                JSONArray jsonArray = obj.getJSONArray("data");
                if(model.size() == 0){
                    fetchComments(jsonArray);
                   JSONObject replyObject = obj.getJSONObject("reply");
                   for(int i=0;i<model.size();i++){
                       JSONArray replyArrays = replyObject.getJSONArray(String.valueOf(model.get(i).comment_id));
                       for(int j=0;j<replyArrays.length();j++){
                           JSONObject jsonObject1 = replyArrays.getJSONObject(j);
                           Integer id = jsonObject1.getInt("id");
                           String name = jsonObject1.getString("name");
                           String comment_image = jsonObject1.getString("photo");
                           Integer user_id = jsonObject1.getInt("user_id");
                           String comment = jsonObject1.getString("comment");
                           String added_on = jsonObject1.getString("addedon");
                           String imageURl = Constant.IMAGE_URL+Constant.COMMENT_USER_IMAGE_URL+comment_image;
                           replyModel.add(new ReplyCommentModel(comment,name,imageURl,added_on,user_id,id));
                       }
                       if(replyModel != null){
                           replyModelMap.put(String.valueOf(model.get(i).comment_id),replyModel);

                       }
                   }
                    Log.e("ReplyModelMapSize","----> "+replyModelMap.size());
                    setUpRecyclerview();
                }else{
                    model.clear();
                    fetchComments(jsonArray);
                    upDateRecyclerView();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

尝试在内部for循环中初始化ReplyModel列表,似乎所有项都被添加到同一列表中,并且所有项都被添加到具有所有键的项中,因此得到具有所有键的整个列表。

                  for(int i=0;i<model.size();i++){
                   JSONArray replyArrays = replyObject.getJSONArray(String.valueOf(model.get(i).comment_id));
                  //Initialise inside the loop 
                  ArrayList<ReplyCommentModel> replyModel = new ArrayList();
                   for(int j=0;j<replyArrays.length();j++){
                       JSONObject jsonObject1 = replyArrays.getJSONObject(j);
                       Integer id = jsonObject1.getInt("id");
                       String name = jsonObject1.getString("name");
                       String comment_image = jsonObject1.getString("photo");
                       Integer user_id = jsonObject1.getInt("user_id");
                       String comment = jsonObject1.getString("comment");
                       String added_on = jsonObject1.getString("addedon");
                       String imageURl = Constant.IMAGE_URL+Constant.COMMENT_USER_IMAGE_URL+comment_image;
                       replyModel.add(new ReplyCommentModel(comment,name,imageURl,added_on,user_id,id));
                   }
                   if(replyModel != null){
                       replyModelMap.put(String.valueOf(model.get(i).comment_id),replyModel);

                   }
               }
  相关解决方案