0

I am trying to make simple like and unlike system but when I press the like button it just goes up I can not unlike,the counter doesn't go down for example if I have 5 likes and I unlike the value stays at 5. I am new to Firebase Android programming and I don't know how to implement this properly I hope someone can help me.

Here is my code, I can post the whole thing if this is not the correct one.

private void setUpVote(final ViewHolder holder) {
        final DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        final String userId = holder.post.getUser_id();
        final String postId = holder.post.getPost_id();
        holder.mUpVote.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                reference.child("up_votes").child(holder.currentUserID).child(holder.post.getPost_id()).setValue("true");
                reference.child("posts").child(holder.post.getPost_id()).child("up_votes").child(holder.currentUserID).setValue("true");
                getUpVote(holder);
                holder.mDownVote.setEnabled(false);
                setUserPoints(userId, holder);
                setPostPoints(postId, holder);
            }

            @Override
            public void unLiked(LikeButton likeButton) {

            }
        });
    }

    private void getUpVote(final ViewHolder holder) {
        String postID = holder.post.getPost_id();
        final DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        DatabaseReference newReference = reference.child("posts").child(postID).child("up_votes");
        newReference.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.
                if (dataSnapshot.hasChild(holder.currentUserID)) {
                    holder.upVotedByCurrentUser = true;
                    setState(holder);
                } else {
                    holder.upVotedByCurrentUser = false;
                }
                long numUpVotes = dataSnapshot.getChildrenCount();

                holder.mCountUpVote.setText(String.valueOf(numUpVotes));
            }


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

    private void setDownVote(final ViewHolder holder) {
        final DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        final String postId = holder.post.getPost_id();
        holder.mDownVote.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                reference.child("down_votes").child(holder.currentUserID).child(holder.post.getPost_id()).setValue("true");
                reference.child("posts").child(holder.post.getPost_id()).child("down_votes").child(holder.currentUserID).setValue("true");
                getDownVote(holder);
                holder.mUpVote.setEnabled(false);
                setPostPoints(postId, holder);
            }

            @Override
            public void unLiked(LikeButton likeButton) {
            }
        });
    }

    private void getDownVote(final ViewHolder holder) {
        String postID = holder.post.getPost_id();
        final DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        DatabaseReference newReference = reference.child("posts").child(postID).child("down_votes");
        newReference.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.
                if (dataSnapshot.hasChild(holder.currentUserID)) {
                    holder.downVotedByCurrentUser = true;
                    setState(holder);
                } else {
                    holder.downVotedByCurrentUser = false;
                }
                long numDownVotes = dataSnapshot.getChildrenCount();

                holder.mCountDownVote.setText(String.valueOf(numDownVotes));
            }

I hope someone can help me!

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

0

This is not the best way in which you can manage this kind of problems. When it comes to likes and unlikes, the first problem that comes up is related to concurrent modifications in which case you should use transactions in order to have accurate data in your database.

So in this case, I recommend you to use my answer from post but instead of score you should use likes. The same principle applies.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • How can I implement this, I am quite new to this stuff? –  Jul 10 '18 at 15:10
  • You should make your own attempt given the information in that answer, and ask another question if something else comes up. – Alex Mamo Jul 10 '18 at 15:12
  • Don't know how to add in the class –  Jul 11 '18 at 10:25
  • 1
    I will try now and ask a question on how do I implement this. –  Jul 11 '18 at 10:30
  • I give up I can't make it work I don't know how to implement it –  Jul 11 '18 at 10:41
  • Never give up. Just add another question so me and other users can help you. – Alex Mamo Jul 11 '18 at 10:43
  • I copied the project from Github and I am trying to rebuild the up vote system because it doesn't work your solution is good but I dont know where to start and where to put the code I can show you the app that I copied and you can take a look and help me –  Jul 11 '18 at 10:47
  • this is the project that I copied:https://github.com/buip/glance/blob/master/app/src/main/java/com/prestonbui/glancesocial/adapter/NewsFeedRecyclerViewAdapter.java –  Jul 11 '18 at 10:47
  • Your project has over 400 lines of code and is hard for me or for other users to debug it online. Let's stick to one problem at a time. So add another question which contains a single issue. – Alex Mamo Jul 11 '18 at 10:58
  • I dont know where to start, could you give me a hand and help me with implementing your code, I am new to firebase, I want to fix the upvote system but I dont know how –  Jul 11 '18 at 11:16
  • When using that code in that exampleand when you encounter problems or errors, it's best to create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and post a new question. – Alex Mamo Jul 11 '18 at 11:20
  • ok thank you I am giving up, I am noob, so sorry for wasting your time. –  Jul 11 '18 at 11:24