如何使用firebase制作upvote和downvote系统?

时间:2018-11-24 17:43:07

标签: android firebase firebase-realtime-database

我正在尝试使用Firebase使Reddit(或Stackoverflow:P)像upvote和downvote系统一样,我尝试了以下操作(当单击like按钮时,它会增加,但是当我不喜欢它时,它不会返回一个值,即正常状态):

private void setUpVote() {
        final DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        upVote.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                reference.child("data").child("-Kxzyb5JsUPhsMQAb84X").child("up_votes").child(currentUserID).child(postID).setValue("true");
                reference.child("data").child("-Kxzyb5JsUPhsMQAb84X").child("posts").child(postID).child("up_votes").child(currentUserID).setValue("true");
                setUserPoints();
                getUpVote();
                setPostPlusPoint(postID);
            }

            @Override
            public void unLiked(LikeButton likeButton) {
                reference.child("data").child("-Kxzyb5JsUPhsMQAb84X").child("up_votes").child(currentUserID).child(postID).removeValue();
                reference.child("data").child("-Kxzyb5JsUPhsMQAb84X").child("posts").child(postID).child("up_votes").removeValue();
                setUserPoints();
                getUpVote();
                setMinusPostPoint(postID);
            }
        });

private void setMinusPostPoint(final String postID) {
    final DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    DatabaseReference newReference = reference.child("data").child("-Kxzyb5JsUPhsMQAb84X").child("posts").child(postID);
    newReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            Post post = dataSnapshot.getValue(Post.class);
            long points = post.getPoints();
            reference.child("data").child("-Kxzyb5JsUPhsMQAb84X").child("posts").child(postID).child("points").setValue(points);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w(TAG, "Failed to read value.", error.toException());
        }
    });

这是我的屏幕快照数据结构:

Screenshot

0 个答案:

没有答案