如何添加类似计数器的Android Studio Firebase [英] How to add like counter Android Studio Firebase
问题描述
这是数据库链接 这种增加类似于计数,但不减少,我希望它在单击时会减少 您的问题尚不完全清楚,但是,如果您尝试创建社交媒体帖子这样的功能,用户可以在其中喜欢和不喜欢该功能,那么这会有所帮助. /p>
数据结构 您可以将其分为三个部分: 要显示帖子的喜欢次数,请 像点击一样点击 这样,您可以使用户喜欢和不喜欢帖子,并显示喜欢的总数.
希望对您有帮助! 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 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 You can divide it in three parts: To display number of likes for a post , On Like Clicked 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屋!Post-> postId -> likes -> 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) {
}
});
}
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) {
}
});
}
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);
}
Post-> postId -> likes -> 1
...
postId
& current user, userId1
, has liked your post or notpublic 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) {
}
});
}
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) {
}
});
}