Firebase Removevalue

Asked

Viewed 325 times

0

I’m tending to keep a list on the id firebase... the intention is when press the follow button if you don’t have the id in the firebase add the id and if you have removed the id.
I can only add and cannot remove, can anyone 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) {
                        }
                    });}

1 answer

0

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

the uid sought:

 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.