I try to set a new value from inside onDataChange
on firebase but when i try to set a new value on a variable inside real-time database the android studio mark the setValue()
method with red and says Cannot resolve method 'setValue' in 'DataSnapshot'
Here is the part of my code:
like_btn.setImageResource(R.drawable.like_emoji);
postRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
// get post id \
PostModel postModel = new PostModel();
String post_id = postModel.getId();
// get current likes from db \
String currentLikes = snapshot.child(post_id).child("likes").getValue().toString();
int currentLikesToInt = Integer.parseInt(currentLikes);
// set new current likes on db \
int newCurrentLikes = currentLikesToInt + 1;
snapshot.child(post_id).child("likes").setValue(newCurrentLikes);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(itemView.getContext(), "Error: " + error, Toast.LENGTH_LONG).show();
}
});
question from:
https://stackoverflow.com/questions/65921359/setvalue-inside-ondatachange 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…