Problem in recovering with Firebase

0

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)

    
asked by anonymous 23.02.2017 / 15:49

2 answers

0

You need to make a similar change.

  mLikebtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick (View v){

            //Aqui você faz a mudança que precisar.
            //Apois mudar, o mDatabaseLike.addValueEventListener(new ValueEventListener() { será chamado de novo.

            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;


            }
    }
}

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.getChildrenCount() + "");

        }
        // mDatabaseLike.child("curtidas").setValue(String.valueOf(snap.child(currentUserId)),snap.getChildrenCount()+ "");

    }


    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
    
23.02.2017 / 17:16
0

Use the method:

@Override <br>OnStart(){
  //Coloque o que você precisa carregar quando iniciar
}; 

It will load what you need first;

    
28.10.2017 / 02:58