What is happening is as follows, when I click on the button (mLikebtn) it adds 1 value to child Like on firebase if I click again remove (Like Like Mechanics) ... the problem is when I exit the current activity and return, or when I quit the app and return, the value of the TextView no longer appears in Firebase, it only appears if you click it again 2x. (because clicking again takes the child off, and comes back when you click it again) .
Thanks to anyone who can help me.
mDatabaseLike.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap: dataSnapshot.getChildren()) {
mCurtidas.setText(dataSnapshot.getChildrenCount() + "");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mLikebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mProcessLike = true;
mDatabaseLike.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (mProcessLike) {
if (dataSnapshot.child(currentUserId).hasChild(mAuth.getCurrentUser().getUid())) {
mDatabaseLike.child(currentUserId).child(mAuth.getCurrentUser().getUid()).removeValue();
mProcessLike = false;
} else {
mDatabaseLike.child(currentUserId).child(mAuth.getCurrentUser().getUid()).setValue("curtidas");
mProcessLike = false;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Update
mDatabaseLike.addValueEventListener(newValueEventListener(){@OverridepublicvoidonDataChange(DataSnapshotdataSnapshot){//Log.e(String.valueOf(dataSnapshot.child(post_key)),dataSnapshot.getChildrenCount()+"");
for (DataSnapshot snap: dataSnapshot.getChildren()) {
//Log.e(String.valueOf(snap.child(currentUserId).toString()),snap.getChildrenCount() + "");
mCurtidas.setText(dataSnapshot.child(currentUserId).getChildrenCount() + "");
}}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
mLikebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mProcessLike = true;
mDatabaseLike.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Log.e(String.valueOf(dataSnapshot.child(post_key)),dataSnapshot.getChildrenCount() + "");
// mDatabaseLike.child("curtidas").setValue(String.valueOf(snap.child(currentUserId)),snap.getChildrenCount()+ "");
if (mProcessLike) {
if (dataSnapshot.child(currentUserId).hasChild(mAuth.getCurrentUser().getUid())) {
mDatabaseLike.child(currentUserId).child(mAuth.getCurrentUser().getUid()).removeValue();
mProcessLike = false;
} else {
mDatabaseLike.child(currentUserId).child(mAuth.getCurrentUser().getUid()).setValue("curtidas");
mProcessLike = false;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
My Unanswered Questions: Problem in recovering data on map (Firebase)