Firebase RemoveValue

0

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) {
                        }
                    });}
    
asked by anonymous 08.04.2017 / 01:11

1 answer

0

Use this line mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");

the requested uid:

 mDatabase = FirebaseDatabase.getInstance().getReference()
.child("Users").child(mAuth.getCurrentUser().getUid());

Then you use the method to know if it is null or not, and decide what will be done.

    
08.04.2017 / 16:44