猿问

如何计算列表中相似的id对象并显示在recyclerview中?

我有一个 JSON 数组,因为我需要使用 ParentCommentID 进行匹配计数,并使用文本视图将数据解析到 recyclerview 中,同时计数我只得到一个位置的数字计数,其他项目也一样,任何帮助。


{

  "d": {

    "results": [

      {

        "__metadata": {

          "id": "e7433825-6771-4f5e-96c7-c4d2674d7764",

          "uri": "",

          "etag": "\"2\"",

          "type": "SP.Data.InquiryDiscussionsListListItem"

        },

        "Author": {

          "__metadata": {

            "id": "f064775c-6161-4bdb-9f4d-8bc6a898d218",

            "type": "SP.Data.UserInfoItem"

          },

          "Title": "Submitter1"

        },

        "Id": 1501,

        "ID": 1501,

        "Title": null,

        "Created": "2019-06-06T04:15:17Z",

        "ParentCommentID": "1439",

        "Comment": "<div class=\"ExternalClass8C333A77B6564A53BED74CA1BA2D2A10\">

        reply add for 009</div>",

        "CommentType": null,

        "CommentDocumentName": null,

        "AppID": "1083",

        "Role": "Customer"

      },

      {

        "__metadata": {

          "id": "e92f708f-93dc-4587-8c4f-5518ed24f360",

          "uri": "",

          "etag": "\"2\"",

          "type": "SP.Data.InquiryDiscussionsListListItem"

        },

        "Author": {

          "__metadata": {

            "id": "209d2d9a-bb07-4064-aaa0-231ad881a80f",

            "type": "SP.Data.UserInfoItem"

          },

          "Title": "Submitter1"

        },

        "Id": 1500,

        "ID": 1500,

        "Title": null,

        "Created": "2019-06-06T04:14:55Z",

        "ParentCommentID": "1439",

        "Comment": "<div class=\"ExternalClass44B1A0BB4D314C57BEE20141BFF10491\">comment add for       009</div>",

        "CommentType": null,

        "CommentDocumentName": null,

        "AppID": "1083",

        "Role": "Customer"

      },

      {

        "__metadata": {

          "id": "ec112002-3132-4bc1-8f85-03fbd9fda11d",

          "uri": "",

          "etag": "\"2\"",

          "type": "SP.Data.InquiryDiscussionsListListItem"

        },

      }

    ]

  }

}


例如 id 1439,有两个ParentCommentID,所以在这种情况下,对于 id1439应该有两条评论,我试图在我的 recyclerview 适配器中使用 for 但它不起作用。


哔哔one
浏览 143回答 1
1回答

吃鸡游戏

我认为您需要遍历结果列表,当您找到具有父评论的结果时,您进入地图,获取父评论,将其计数加一并将其粘贴回地图中:假设您的 Result 类是这样的:class Result {&nbsp; &nbsp; private String id;&nbsp; &nbsp; private String parentCommentID;&nbsp; &nbsp; public Result(String id, String parentCommentID) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; this.parentCommentID = parentCommentID;&nbsp; &nbsp; }&nbsp; &nbsp; // GETTERS/SETTERS}你有一个包含 3 个结果的讨论列表&nbsp; &nbsp; discussionsList = Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; new Result("1439", null),&nbsp; &nbsp; &nbsp; &nbsp; new Result("1500", "1439"),&nbsp; &nbsp; &nbsp; &nbsp; new Result("1801", "1439")&nbsp; &nbsp; );像你的情况一样, 1439 没有父母, 1500 和 1501 都有 1439 作为父母然后你可以做这样的事情:Map<String, Integer> commentsCountMap = new HashMap<>();// Loop through the discussion Mapfor(Result res : discussionsList) {&nbsp; &nbsp; String parentCommentId = res.getParentCommentID();&nbsp; &nbsp; // If the Result has a parent comment&nbsp; &nbsp; if(parentCommentId != null) {&nbsp; &nbsp; &nbsp; &nbsp; // get the count for this parent comment (default to 0)&nbsp; &nbsp; &nbsp; &nbsp; int nbCommentsForParent = commentsCountMap.getOrDefault(parentCommentId, 0);&nbsp; &nbsp; &nbsp; &nbsp; // increment the count&nbsp; &nbsp; &nbsp; &nbsp; nbCommentsForParent++;&nbsp; &nbsp; &nbsp; &nbsp; // Update the Map with the new count&nbsp; &nbsp; &nbsp; &nbsp; commentsCountMap.put(parentCommentId, nbCommentsForParent);&nbsp; &nbsp; }}System.out.println(commentsCountMap);这输出{1439=2}
随时随地看视频慕课网APP

相关分类

Java
我要回答