java - 当我单击“like”按钮时,它不会调用设置要调用的方法

标签 java android firebase google-cloud-firestore adapter

当我单击“like”按钮时,它不会调用设置为在适配器中调用的方法,它会在 logcat“viewposttime 指针 0”和“viewposttime 指针 1”中提供输出。我用另一个适配器做了类似的事情,效果很好。不知道为什么它没有调用该方法,Log.D 在此工作正常。我想要的只是当单击按钮时它执行 l1comment() 的功能来更新数据库

我关心的是 onBindViewHolder 中的 FavPostButton

下面是代码

protected void getComments() {
    Query query = db.collection("posts").document(Post_Key).collection("comments");
    FirestoreRecyclerOptions<Comments> options = new FirestoreRecyclerOptions.Builder<Comments>()
            .setQuery(query, Comments.class)
            .build();

    adapter = new FirestoreRecyclerAdapter<Comments, CommentsViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull CommentsViewHolder commentsViewHolder, int i, @NonNull Comments comments) {
            documentId = getSnapshots().getSnapshot(i).getId();

            commentsViewHolder.textViewTitle.setText(comments.getUsername());
            commentsViewHolder.textViewDescription.setText(comments.getComment());

            commentsViewHolder.favPostButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    l1Comment();
                    Log.d(TAG, "Your CommentID" + documentId);
                }
            });
        }


        @NonNull
        @Override
        public CommentsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.comment_list_items, parent, false);

            return new CommentsViewHolder(view);

        }

        @Override
        public void onError(FirebaseFirestoreException e) {
            Log.e("error", e.getMessage());
        }
    };

    adapter.notifyDataSetChanged();
    commentsList.setAdapter(adapter);


}

private void l1Comment() {


    likesRef = db.collection("posts").document(Post_Key).collection("comments").document(documentId);

    likesRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {

            if (documentSnapshot.contains(mUserId)) {
                likesRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            DocumentSnapshot document = task.getResult();
                            if (document.exists()) {
                                if (document.get("l1") != null) {
                                    Toast.makeText(CommentActivity.this, "You already in Process", Toast.LENGTH_SHORT).show();
                                    Log.d(TAG, "your field exist");
                                } else {
                                    Log.d(TAG, "your field does not exist");
                                    likeChecker = true;
                                    Map<String, Object> commentMap = new HashMap<>();
                                    commentMap.put("l1", likeChecker);
                                    likesRef.update(commentMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if (task.isSuccessful()) {

                                                Toast.makeText(CommentActivity.this, "l1 done", Toast.LENGTH_SHORT).show();
                                            } else {
                                                Toast.makeText(getApplicationContext(), task.getException().getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                                            }

                                        }
                                    });

                                    //Create the filed
                                }
                            }

                        }
                    }
                });

            } else {

            }

        }
    });
}

public static class CommentsViewHolder extends RecyclerView.ViewHolder {

    View mView;

    TextView textViewTitle;
    TextView textViewDescription;
    TextView textViewPriority;
    Button favPostButton;
    Button commentsbutton;


    public CommentsViewHolder(final View itemView) {
        super(itemView);
        textViewTitle = itemView.findViewById(R.id.list_comment_name);
        textViewDescription = itemView.findViewById(R.id.list_comment_content);
        textViewPriority = itemView.findViewById(R.id.list_comment_stamp);
        commentsbutton = itemView.findViewById(R.id.comments_input_button);
        favPostButton = itemView.findViewById(R.id.comment_liking);


    }
}

最佳答案

使用以下代码并在 firebase 仪表板中查看结果。

private void l1Comment() {
    likesRef = db.collection("posts").document(Post_Key).collection("comments").document(documentId);
    likesRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
            if (documentSnapshot.contains(mUserId)) {
                likesRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            DocumentSnapshot document = task.getResult();
                            if (document.exists()) {
                                if (document.get("l1") != null) {
                                    Toast.makeText(CommentActivity.this, "You already in Process", Toast.LENGTH_SHORT).show();
                                    Log.d(TAG, "your field exist");
                                } else {
                                    new Thread("update_thread"){
                                        public void run(){
                                            Log.d(TAG, "your field does not exist");
                                            likeChecker = true;
                                            Map<String, Object> commentMap = new HashMap<>();
                                            commentMap.put("l1", likeChecker);
                                            likesRef.update(commentMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                                                @Override
                                                public void onComplete(@NonNull Task<Void> task) {
                                                    if (task.isSuccessful()) {
                                                        Toast.makeText(CommentActivity.this, "l1 done", Toast.LENGTH_SHORT).show();
                                                    } else {
                                                        Toast.makeText(getApplicationContext(), task.getException().getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                                                    }
                                                }
                                            });
                                            //Create the filed
                                        }
                                    }.start();
                                }
                            }
                        }
                    }
                });
            } else {
            }
        }
    });
}

关于java - 当我单击“like”按钮时,它不会调用设置要调用的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57755271/

相关文章:

java - 从java运行交互式bat文件?

android - OAuth token 无效 : Error 2500

javascript - 在 Firebase Web 中获取正确的子级

firebase - Flutter:将图像上传到Firebase存储

java - 如何编写排除某个范围值的正则表达式?

java - 使用 Apache POI 的 SXSSFSheet 从 xlsx 读取数据

java - Collection 方法的定义位于哪里?

android - Android上如何正确读取AsyncStorage

java - osmdroid - bundle 离线 map 图 block

node.js - 当用户进入地理围栏/区域时向用户发送消息?