我正在尝试制作简单的“与众不同”系统,但是当我按“喜欢”按钮时,它只会上升,我无法与众不同,例如,如果我有5个“喜欢”而我的价值保持不变,则计数器不会下降5.我是Firebase Android编程的新手,我不知道如何正确实现这一点,希望有人可以帮助我。
这是我的代码,如果这不是正确的代码,则可以发布整个代码。
private void setUpVote(final ViewHolder holder) {
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
final String userId = holder.post.getUser_id();
final String postId = holder.post.getPost_id();
holder.mUpVote.setOnLikeListener(new OnLikeListener() {
@Override
public void liked(LikeButton likeButton) {
reference.child("up_votes").child(holder.currentUserID).child(holder.post.getPost_id()).setValue("true");
reference.child("posts").child(holder.post.getPost_id()).child("up_votes").child(holder.currentUserID).setValue("true");
getUpVote(holder);
holder.mDownVote.setEnabled(false);
setUserPoints(userId, holder);
setPostPoints(postId, holder);
}
@Override
public void unLiked(LikeButton likeButton) {
}
});
}
private void getUpVote(final ViewHolder holder) {
String postID = holder.post.getPost_id();
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
DatabaseReference newReference = reference.child("posts").child(postID).child("up_votes");
newReference.addValueEventListener(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.
if (dataSnapshot.hasChild(holder.currentUserID)) {
holder.upVotedByCurrentUser = true;
setState(holder);
} else {
holder.upVotedByCurrentUser = false;
}
long numUpVotes = dataSnapshot.getChildrenCount();
holder.mCountUpVote.setText(String.valueOf(numUpVotes));
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
}
private void setDownVote(final ViewHolder holder) {
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
final String postId = holder.post.getPost_id();
holder.mDownVote.setOnLikeListener(new OnLikeListener() {
@Override
public void liked(LikeButton likeButton) {
reference.child("down_votes").child(holder.currentUserID).child(holder.post.getPost_id()).setValue("true");
reference.child("posts").child(holder.post.getPost_id()).child("down_votes").child(holder.currentUserID).setValue("true");
getDownVote(holder);
holder.mUpVote.setEnabled(false);
setPostPoints(postId, holder);
}
@Override
public void unLiked(LikeButton likeButton) {
}
});
}
private void getDownVote(final ViewHolder holder) {
String postID = holder.post.getPost_id();
final DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
DatabaseReference newReference = reference.child("posts").child(postID).child("down_votes");
newReference.addValueEventListener(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.
if (dataSnapshot.hasChild(holder.currentUserID)) {
holder.downVotedByCurrentUser = true;
setState(holder);
} else {
holder.downVotedByCurrentUser = false;
}
long numDownVotes = dataSnapshot.getChildrenCount();
holder.mCountDownVote.setText(String.valueOf(numDownVotes));
}
我希望有人能帮助我!
答案 0 :(得分:0)
这不是解决此类问题的最佳方法。当涉及到喜欢和不同时,出现的第一个问题与并发修改有关,在这种情况下,您应该使用事务来在数据库中获得准确的数据。
因此,在这种情况下,我建议您使用 post 中的答案,但应该使用喜欢代替得分。同样的原则也适用。