How to to hide josn object in recyclerview based on condition?

房东的猫 提交于 2019-12-11 14:49:12

问题


I Have one json array in array list i need show one object as count and one as text view, while going to filter the object i'm not able to find count view and display view, i search for hash map but its does not work, my question i can i add two array list in one recyclerview adapter one for view and one for count in same view. Edit: after researching about that i come to know i need to hide object if it is a match with id but i could not found to display and hide content base on condition in recyclerview any Solution?

@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 object generated is as below

      {
      "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"
    }
   ]
   }
    }

In Above Json response if object is Comment i want to display content but if it is a reply i need to display count base on id match any solution?


回答1:


You can achieve this in a simple way first pass both the list to your adapter then return the total size

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

Now for onBindViewHolder for example you want to display mList first then

  @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

}
}



回答2:


I am not sure I understand you fully. However, In my opinion, you can create a model class and keep two variables there.

public class Demo {
    private int count;
    private int value;

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

Then, you can create an ArrayList of that model class and pass it to RecyclerView adapter.

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);

Feel free to ask if I'm not clear or anything else.



来源:https://stackoverflow.com/questions/56536469/how-to-to-hide-josn-object-in-recyclerview-based-on-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!