我在這裏很困惑,真的到了最後,我在我的項目中設置了一個類似的功能。如果用戶按下同樣的按鈕一次,類似計數器從0更新爲1(喜歡)等imageButton(更改顏色)更新成功。如果按兩次計數器從1更新到0(不同)成功。Firebase like counter
問題是,當一個不同的用戶也按類似的按鈕喜歡相同的職位,類似的計數器不會更新從1到2.請幫助。我希望這很清楚。以下是代碼。
viewHolder.mLikebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mProcessLike = true;
mDatabaseLikeCount = FirebaseDatabase.getInstance().getReference().child("Notes").child(post_key).child("likecount");
mDatabaseLikeCount.keepSynced(true);
mDatabaseLike.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (mProcessLike) {
if (dataSnapshot.child(post_key).hasChild(auth.getCurrentUser().getUid())) {
Log.i("D Diary", "User has already Liked. So it can be considered as Unliked.");
mDatabaseLike.child(post_key).child(auth.getCurrentUser().getUid()).removeValue();
mDatabaseLikeCount.setValue(likeCount = likeCount - 1);
mProcessLike = false;
} else {
Log.i("D Diary", "User Liked");
mDatabaseLike.child(post_key).child(auth.getCurrentUser().getUid()).setValue(auth.getCurrentUser().getDisplayName());
mDatabaseLikeCount.setValue(likeCount = likeCount + 1);
Log.i(dataSnapshot.getKey(), dataSnapshot.getChildrenCount() + "Count");
mProcessLike = false;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
你能告訴你如何定義likeCount變量? – GAlexMES
在POJO它初始化爲0 –