0
What is happening is this, when I click the button(mLikebtn) it adds 1 value to Child Like in firebase if I click again remove(Mechanics of Like same)... the problem is when I leave the current activity and return , or when I leave the app and return, The Textview value no longer appears in Firebase , only appears if you click it again 2x. (because if you click again remove Child, and it comes back when you click again).
I thank 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) {
}
});
Updating
mDatabaseLike.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//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 Questions without Answers: Problem in recovering data in map (Firebase)
Your Switch is inside the onclick button. Put it out every time you change something in firebases, it will be called. Use Onclick to change. To recover use firebase listerner, anywhere.
– Icaro
I couldn’t keep up..
– Wallace Roberto
in case I would have to do one for that chunk...for (Datasnapshot snap: dataSnapshot.getChildren()) { //Log. e(String.valueOf(snap.Child(currentUserId).toString()),snap.getChildrenCount() + ""); mCurtidas.setText(dataSnapshot.getChildrenCount() + "); ?
– Wallace Roberto
You are missing the logic of Databases. Listeners need to stay in the main Class. It keeps listening to the database, and any change it changes and calls the public void onDataChange(Datasnapshot dataSnapshot) {} .
– Icaro