Tell Likes Firebase Android

Asked

Viewed 281 times

1

So I’m with a project here from an appblog, inserir a descrição da imagem aqui

I need help now so I can count the likes.

    mDatabaseLike = FirebaseDatabase.getInstance().getReference().child("Likes");


  }
        });

        viewHolder.mLikeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mProcessLike = true;


                    mDatabaseLike.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                              if (mProcessLike ){

                                    if(dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())){

                                        mDatabaseLike.child(post_key).child(mAuth.getCurrentUser().getUid()).removeValue();
                                        mProcessLike = false;
                                    } else {

                                         mDatabaseLike.child(post_key).child(mAuth.getCurrentUser().getUid()).setValue("RandomValue");
                                        mProcessLike = false;
                                    }

                        }}

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

                }
        });

    }
};

inserir a descrição da imagem aqui

http://pastebin.com/JKCa4feS

RESOLVED

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

                }
            });
  • What a problem you’re having?

  • No problem, I don’t know how to count the likes, the codex is all right, but there’s no line on how to count the likes...

  • You will need to use a Switch, as soon as you open Activity to find out if you’ve already liked it. If you do, click remove, if you don’t like it. In your print it looks like it’s working. If you’re in doubt about the amount of likes. You just need to take the "branch" likes and make a size();

  • Okay, the part of the button is already clickable.

  • Do you have any idea how I do it so that you tell . Hild from firebase?

  • You must have a class, which encapsulates the content of the blog post. Adds a variable, which counts the Likes. It can be a list or even an int. https://firebase.google.com/docs/database/android/start/

Show 1 more comment

1 answer

0

You must have a class, which encapsulates the content of the blog post. Adds a variable, which counts the Likes. It can be a list or even an int. In the Database Reader, you capture the content you have in the database.

   // Read from the database
    myRef.addValueEventListener(new ValueEventListener() {
   @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    // This method is called once with the initial value and again
    // whenever data at this location is updated.
    SuaClasse  class = dataSnapshot.getValue(SuaClass.class);

    }

    @Override
     public void onCancelled(DatabaseError error) {
    // Failed to read value
    Log.w(TAG, "Failed to read value.", error.toException());
   }
  });

https://firebase.google.com/docs/database/android/start/

Browser other questions tagged

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