I'm tending to keep a list in the id firebase ... the intention is when hit the follow button if you do not have the id in the firebase add the id and if you have remove the id. I can only add and can not remove, can someone help me? mPost_key is the user id.
private boolean mProcessSeguindo = false;
mProcessSeguindo = true;
private String mPost_key = null;
----
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
----
mProcessSeguindo = true;
final String user_idd = mAuth.getCurrentUser().getUid();
mSeguir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final DatabaseReference newComent = mDatabase.push();
mDatabase.child(mPost_key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (mProcessSeguindo) {
if (dataSnapshot.child(user_idd).hasChild(mPost_key)) {
mDatabase.child(user_idd).child(mPost_key).removeValue();
mProcessSeguindo = false;
} else {
mDatabase.child(user_idd).child(mPost_key).setValue("seguindo");
mProcessSeguindo = false;
}}}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
RESOLVED
private boolean mProcessSeguindo = false;
mSeguir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mProcessSeguindo = true;
mDatabase.child(mPost_key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String post_cont = (String) dataSnapshot.child("name").getValue();
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (mProcessSeguindo) {
if (dataSnapshot.child(user_id).child("seguindo").hasChild(currentUserId)) {
mDatabase.child(user_id).child("seguindo").child(currentUserId).removeValue();
mProcessSeguindo = false;
mSeguir.setBackgroundColor(getResources().getColor(R.color.gray));
mSeguir.setText(getResources().getText(R.string.seguir));
} else {
mDatabase.child(user_id).child("seguindo").child(currentUserId).child("name").setValue(post_cont);
mSeguir.setBackgroundColor(getResources().getColor(R.color.green));
mSeguir.setText(getResources().getText(R.string.seguindo));
mProcessSeguindo = false;}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});}