如何添加类似计数器的Android Studio Firebase [英] How to add like counter Android Studio Firebase

查看:149
本文介绍了如何添加类似计数器的Android Studio Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是数据库链接

这种增加类似于计数,但不减少,我希望它在单击时会减少

您的问题尚不完全清楚,但是,如果您尝试创建社交媒体帖子这样的功能,用户可以在其中喜欢和不喜欢该功能,那么这会有所帮助. /p>

数据结构

Post-> postId -> likes -> 1 
              ...

您可以将其分为三个部分:

  1. 要显示帖子的喜欢次数,请postId&当前用户userId1是否喜欢您的帖子

    public void displayNumberOfLikes(String postId, String currentUserId){
        DatabaseReference likesRef = FirebaseDatabase.getInstance().getReference().child('Post').child(postId);
        likesRef.addValueEventListener(new ValueEventListener(){
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                    long numOfLikes = 0;
                    if(dataSnapshot.hasChild("likes")){
                        numOfLikes = dataSnapshot.child("likes").getValue(Long.class);
                    }
    
                    //Populate numOfLikes on post i.e. textView.setText(""+numOfLikes)
                    //This is to check if the user has liked the post or not
                    btnLike.setSelected(dataSnapshot.hasChild(userId));
                }
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    }
    

  2. 像点击一样点击

    public void onLikeClicked(View v, String postId, String userId){
        DatabaseReference likesRef = FirebaseDatabase.getInstance().getReference().child('Post').child(postId).child("likes");
        likesRef.addListenerForSingleValueEvent(new ValueEventListener(){
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                long numLikes = 0;
                if(dataSnapshot.exists()){
                    numLikes = dataSnapshot.getValue(Long.class);
                }
                boolean isLiked = btnLike.isSelected();
                if(isLiked){
                   //If already liked then user wants to unlike the post
                   likesRef.set(numLikes-1);
                }else {
                   //If not liked already then user wants to like the post
                   likesRef.set(numLikes+1);
                }
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    }
    

这样,您可以使用户喜欢和不喜欢帖子,并显示喜欢的总数. 希望对您有帮助!

this is the database link 1can anybody help me to add a like counter to this.please please it should display the no:of likes and decease it when like is taken back my java code is below

 private void onLikeClicked(View v) {
    boolean isLiked = !btnLike.isSelected();
    final String currentUserKey = User.currentKey();

    DatabaseReference likes = FirebaseDatabase.getInstance().getReference().child(Const.kDataLikeKey);
    DatabaseReference curLike = likes.child(mPostRef.getKey()).child(currentUserKey).child("liked");

    // update Model
    curLike.setValue(isLiked);

    // update UI
    btnLike.setSelected(isLiked);
}

This increment like count but doesn't decrements it i want it to get decremented when clicked back

解决方案

Your question is not entirely clear but if you are trying to make a functionality like social media post where users can like and unlike a post the this can help.

Data Structure

Post-> postId -> likes -> 1 
              ...

You can divide it in three parts:

  1. To display number of likes for a post , postId & current user, userId1, has liked your post or not

    public void displayNumberOfLikes(String postId, String currentUserId){
        DatabaseReference likesRef = FirebaseDatabase.getInstance().getReference().child('Post').child(postId);
        likesRef.addValueEventListener(new ValueEventListener(){
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                    long numOfLikes = 0;
                    if(dataSnapshot.hasChild("likes")){
                        numOfLikes = dataSnapshot.child("likes").getValue(Long.class);
                    }
    
                    //Populate numOfLikes on post i.e. textView.setText(""+numOfLikes)
                    //This is to check if the user has liked the post or not
                    btnLike.setSelected(dataSnapshot.hasChild(userId));
                }
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    }
    

  2. On Like Clicked

    public void onLikeClicked(View v, String postId, String userId){
        DatabaseReference likesRef = FirebaseDatabase.getInstance().getReference().child('Post').child(postId).child("likes");
        likesRef.addListenerForSingleValueEvent(new ValueEventListener(){
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                long numLikes = 0;
                if(dataSnapshot.exists()){
                    numLikes = dataSnapshot.getValue(Long.class);
                }
                boolean isLiked = btnLike.isSelected();
                if(isLiked){
                   //If already liked then user wants to unlike the post
                   likesRef.set(numLikes-1);
                }else {
                   //If not liked already then user wants to like the post
                   likesRef.set(numLikes+1);
                }
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    }
    

This way you can make your user like and unlike a post and display total number of likes. Hope it helps!

这篇关于如何添加类似计数器的Android Studio Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆