How to add like counter Android Studio Firebase


How to add like counter Android Studio Firebase



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




1 Answer
1



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:



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


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 = 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) {

}
});
}



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!





Thank you sir but this is having a small issue when i unlike it that node is deleted..so say if 2 users have liked it and when one of then press back(unlike) then the whole node is deleted..actually i just want to subtract 1 from the whole like count when the user pres like button again
– Arya Prabagar
Jun 29 at 15:30






Can you add your db structure in the question? It would be helpful if you explain the exact scenario using actual data.
– mark922
Jun 29 at 19:44





posts -LGEF7JPjhzROruGct_E -image: -likecnt -likecnt: -likes: -time: -user:
– Arya Prabagar
Jun 30 at 6:18





@AryaPrabagar This is not helping. Can put it in a fair image with some explanation
– mark922
Jun 30 at 8:41





sir i have added a image link
– Arya Prabagar
Jun 30 at 9:42






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux