【问题标题】:Show specific comments from adapter in recyclerview在 recyclerview 中显示来自适配器的特定注释
【发布时间】:2020-05-06 01:40:29
【问题描述】:

我正在使用带有 FirestoreRecyclerAdapter 的模型在一个 android 应用程序上进行评论会话。评论存储在 Firestore 上,我使用模型和适配器来显示存储在 text_review 中的 cmets 文本。这是我的模型:

public class Comment {
    float fireness_rating;
    String post_id;
    String review_time;
    String receiver_id;
    String text_review;


    public Comment(){
        //empty const needed.
    }

    Comment(float fireness_rating, String post_id, String review_time, String receiver_id, String text_review){
        this.fireness_rating=fireness_rating;
        this.post_id=post_id;
        this.review_time=review_time;
        this.receiver_id=receiver_id;
        this.text_review=text_review;

    }

    public float getFireness_rating() {
        return fireness_rating;
    }

    public String getPost_id() {
        return post_id;
    }

    public String getReview_time() {
        return review_time;
    }

    public String getReceiver_id() {
        return receiver_id;
    }

    public String getText_review() {
        return text_review;
    }
}


该模型包含五个元素,但我主要关注 text_review。如果用户内部有文本 cmets,则 text_review 将包含用户键入并存储在 firestore 上的文本。这是用户使用 text_review 完成后的 firestore 结构。

正如我所提到的,如果里面没有实际的文本评论,那么 text_review 应该是空的。我已经构建了一个适配器来递归所有 cmets 列表以显示在我的活动页面上。这是我的适配器。

public class Comment_Adapter extends FirestoreRecyclerAdapter<Comment, Comment_Adapter.CommentHolder> {

    public Comment_Adapter(@NonNull FirestoreRecyclerOptions<Comment> options){
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull final CommentHolder holder, int i, @NonNull Comment model) {

        final String commented_userid = model.getReceiver_id();
        Log.d("comment_adapter_debug", commented_userid);

        //shown only if the use has comment which in text_review
        Query q = FirebaseFirestore.getInstance().collection("user_reviews").document(commented_userid).collection("fairness").whereGreaterThan("text_review", "");
        q.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                Log.d("comment_adapter_debug", "has comment inside x");

                //retrieve info, you can ignore this part.               FirebaseFirestore.getInstance().collection("users").document(commented_userid).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            DocumentSnapshot document = task.getResult();
                            String first_name = document.get("first_name").toString();
                            String last_name = document.get("last_name").toString();
                            String username = document.get("username").toString();
                            Log.d("comment_adapter_debug", first_name + " " + last_name + ": w/ username= " + username);

                            holder.CommentedFullName.setText(first_name + " " + last_name);
                            holder.CommentedUser.setText("@" + username);
                        } else {
                            Log.d("comment_adapter_debug", "no comment yet for this user.");
                        }

                    }
                });
            }

        });
        String oldstring = model.getReview_time();
        Date dt = new Date(oldstring);
        String timeStamp = new SimpleDateFormat("MMM dd, yyyy").format(dt);
        holder.CommentedDate.setText(timeStamp);
        holder.Comment.setText(model.getText_review());

    }

    @NonNull
    @Override
    public CommentHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_comment, parent, false);
        return new CommentHolder(v);
    }

    class CommentHolder extends RecyclerView.ViewHolder{
        private ImageView CommentedIcon;
        private TextView CommentedFullName;
        private TextView CommentedUser;
        private TextView CommentedDate;
        private TextView Comment;

        public CommentHolder(View itemView){
            super(itemView);
            CommentedIcon = itemView.findViewById(R.id.commented_icon);
            CommentedFullName = itemView.findViewById(R.id.commented_fullname);
            CommentedUser = itemView.findViewById(R.id.commented_user);
            CommentedDate = itemView.findViewById(R.id.commented_date);
            Comment = itemView.findViewById(R.id.commented);
        }
    }

}

这是图片

我想要的只是显示有实际评论(图片中的“aaa”),并且不应该显示红色圆圈,因为不存在评论(“text_review”==“”)。我的方法是查询 whereGreaterThan("text_review", "") 这意味着检查 text_review 是否包含文本。如果是,则显示列表,否则,跳过整个列表。我知道它有点长,但任何帮助将不胜感激。

【问题讨论】:

  • 嗨@zionnoizy 你能编辑你的问题并添加你的收藏的结构/方案吗?这样,可以更轻松地检查文档的组织和存储方式。
  • 我为问题添加了更多细节,谢谢提醒。

标签: android android-recyclerview google-cloud-firestore


【解决方案1】:

感谢您编辑您的问题!

考虑到您的数据库,我会尝试使用以下代码示例来验证 text_review 是否具有正确打印它的价值。

...
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
    if (task.isSuccessful()) {
        DocumentSnapshot document = task.getResult();
        String first_name = document.get("first_name").toString();
        String last_name = document.get("last_name").toString();
        String username = document.get("username").toString();
        if (document.exists()) {
            if (document.get("text_review") != null) {
                Log.d("comment_adapter_debug", first_name + " " + last_name + ": w/ username= " + username);
                holder.CommentedFullName.setText(first_name + " " + last_name);
                holder.CommentedUser.setText("@" + username);
            } else {
                Log.d("comment_adapter_debug", "no comment yet for this user.");
            }
        }
    }
}
...

虽然此代码未经测试,但我相信它应该对您有所帮助。正如您可以检查的那样,您将检查document.get("text_review") 部分,如果值是null,并根据它,根据需要打印信息。如果不是,它将打印出用户还没有评论 - 或者您可以根据需要更改最后一个 Log.d

如果这些信息对您有帮助,请告诉我!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-28
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多