【问题标题】:How to hide json object in recyclerview based on condition?如何根据条件在recyclerview中隐藏json对象?
【发布时间】:2019-10-25 10:46:40
【问题描述】:

我在数组列表中有一个 json 数组我需要显示一个对象作为计数和一个作为文本视图,同时要过滤对象我无法找到计数视图和显示视图,我搜索哈希图但是它不起作用,我的问题是我可以在一个 recyclerview 适配器中添加两个数组列表,一个用于查看,一个用于在同一视图中计数。 编辑:经过研究后,我知道如果对象与 id 匹配,我需要隐藏对象,但我找不到根据 recyclerview 中的条件显示和隐藏内容的任何解决方案?

@Override
public void onBindViewHolder(@NonNull DiscussingAdapter.DiscussionView holder, int position) {
    HashMap<String, List<Result>> hashMap = new HashMap<String, List<Result>>();
    Result result = discussionsList.get(position);
    //discussionsList.get(position);

    List<Result> filterList = discussionsList.stream()
            .filter(p -> p.getParentCommentID()==null).collect(Collectors.toList());
    Log.d("filterList",filterList.toString());
    for (int counter = 0; counter < filterList.size(); counter++) {
        holder.tvHeader.setText(filterList.get(counter).getTitle());
        holder.tvDetail.setText(Utils.html2text(filterList.get(counter).getComment()));
        holder.tvViewReply.setText("Reply");
        holder.tvUser.setText("By " + filterList.get(counter).getAuthor().getTitle());
        String DateofReceipt = filterList.get(counter).getCreated();
        String date = DateofReceipt.split("T", 0)[0];
        String date_before = date;
        String date_after = Utils.date(date_before);
        holder.tvDate.setText(date_after);
    }

    Map<String, Integer> commentsCountMap = new HashMap<>();
    for(Result res : discussionsList) {
        String parentCommentId = res.getParentCommentID();
        // If the Result has a parent comment
        if(parentCommentId != null) {
            // get the count for this parent comment (default to 0)
            int nbCommentsForParent = commentsCountMap.getOrDefault(parentCommentId, 0);
            // increment the count
            nbCommentsForParent++;
            // Update the Map with the new count
            commentsCountMap.put(parentCommentId, nbCommentsForParent);


        }
    }

    for(Map.Entry<String,Integer> cCount : commentsCountMap.entrySet()){

        holder.tvViewReply.setText(String .valueOf(cCount.getValue())+" Reply");



    }}

生成的Json对象如下

      {
      "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"
    },
    "Author": {
      "__metadata": {
        "id": "6e8ecb1d-4deb-4168-a8b4-a725abf8002a",
        "type": "SP.Data.UserInfoItem"
      },
      "Title": "Sarada Devi Potti"
    },
    "Id": 1439,
    "ID": 1439,
    "Title": "Canceled",
    "Created": "2019-06-03T09:32:34Z",
    "ParentCommentID": null,
    "Comment": "<div  class=\"ExternalClass5E1BFEDC348C43719AD940E644E0E0B6\">sdaeadfasdf</div>",
    "CommentType": "Public",
    "CommentDocumentName": null,
    "AppID": "1083",
    "Role": "Budget Analyst"
    }
   ]
   }
    }

在上面的 Json 响应中,如果对象是 Comment,我想显示内容,但如果是回复,我需要根据 id 显示计数匹配任何解决方案?

【问题讨论】:

    标签: java android arraylist android-recyclerview


    【解决方案1】:

    我不确定我是否完全理解你。但是,在我看来,您可以创建一个模型类并在其中保留两个变量。

    public class Demo {
        private int count;
        private int value;
    
        public Demo(int count, int value) {
            this.count = count;
            this.value = value;
        }
    }
    

    然后,您可以创建该模型类的ArrayList 并将其传递给RecyclerView 适配器。

    ArrayList<Demo> demos = new ArrayList<>();
    Demo firstObject = new Demo(1, 10);
    demos.add(firstObject);
    Demo secondObject = new Demo(2, 11);
    demos.add(secondObject);
    Demo thirdObject = new Demo(3, 12);
    demos.add(thirdObject);
    

    如果我不清楚或其他什么,请随时询问。

    【讨论】:

    • 在我的 json 响应中,我需要在一个视图中解析拖曳类型数据,例如,如果 parranet id 为空,那么它将显示为回复,如果评论 id 有一些 id,那么我需要匹配相同的 id 然后显示对象的数量作为计数我没有将此对象添加到 recyclerview 这就是我选择过滤器的原因。
    • 在这种情况下,如果对象作为评论,我们需要显示内容如果对象作为回复,我们需要根据 ParentCommentID 给出计数。
    • 在我看来。您应该首先在活动中解析json。然后用结果创建一个ArrayList。然后,在RecyclerView 中取两个ViewHolder。一个用于如果 id 匹配,另一个用于不匹配。然后在RecyclerViewonBindViewHolder方法中查看。
    【解决方案2】:

    您可以通过简单的方式实现此目的,首先将列表传递给您的适配器 然后返回总大小

      @Override
    public int getItemCount() {
        return mList.size() + mList1.size();
    }
    

    现在以onBindViewHolder 为例,您想先显示mList,然后

      @Override
    public void onBindViewHolder(@NonNull DiscussingAdapter.DiscussionView holder, int position) {
    if(position < mList.size()){
    // Load the data from mList 
    holder.tvName.setText(mList.get(position).getName())
    }else{
    //For getting Data from secondList
    holder.tvName.setText(mList1.get(position-mList.getSize()).getName())
    // Now you will get data from second List
    
    }
    }
    

    【讨论】:

    • 我需要两个类型的数据而不是两个数据我需要第一个过滤数组列表,它没有 parantId 来显示数据和第二个 parrant id 数据。
    猜你喜欢
    • 2021-09-04
    • 2021-05-21
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2018-04-23
    • 1970-01-01
    相关资源
    最近更新 更多