【问题标题】:When I click on the like button it doesn't call the method which set to be invoked当我单击like按钮时,它不会调用设置为调用的方法
【发布时间】:2019-09-02 10:22:30
【问题描述】:

当我点击like按钮时,它不会调用设置为在适配器中调用的方法,它会在logcat“viewposttime pointer 0”和“viewposttime pointer 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);


    }
}

【问题讨论】:

  • 您检查favPostButton 的onClick 事件是否正常工作?一个小建议是,将documentId 作为l1Comment() 的参数发送。
  • 是的,onclick 工作正常,而作为参数的 documenId 不起作用
  • 您是否使用中断指针调试过您的代码?如果您没有,请尝试找出您的代码不起作用的行。也许你错过了一些愚蠢的东西。
  • 调试时显示“已跳过 2313 帧!应用程序可能在其主线程上做的工作过多。”
  • 在不同的线程(不在主线程)运行likesRef.update(commentMap)操作。

标签: java android firebase google-cloud-firestore adapter


【解决方案1】:

使用以下代码并在 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 {
            }
        }
    });
}

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多