Problem recovering with Firebase

Asked

Viewed 257 times

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) {

        }
    });

inserir a descrição da imagem aqui

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.

  • I couldn’t keep up..

  • 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() + "); ?

  • 1

    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) {} .

2 answers

0

Use the method:

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

He’ll carry what you need first;

0


You need to make a change like this.

  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) {

    }
});
  • I haven’t yet continued the same way,]

  • I followed your concept of your reasoning and it worked out worth!

  • I didn’t really understand why it didn’t work at first,mCurtidas.setText(dataSnapshot.Child(currentUserId).getChildrenCount() + ""); I changed this line to pick up the like by post id and already gave the difference together

  • worked, worked?

  • Yes, Thank you very much!

Browser other questions tagged

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