0

I'm confused here and really at the end on the line, I have set up a like function in my project. if a user presses the like button once, the like counter update from 0 to 1(liked) and the like imageButton(change color) updates successful. if pressed twice the counter updates from 1 to 0(unlike) successful.

The problem is when a different user also press the like button to like the same post, the like counter does not update from 1 to 2. Please help. I hope this is clear. Below is the code.

viewHolder.mLikebtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mProcessLike = true; mDatabaseLikeCount = FirebaseDatabase.getInstance().getReference().child("Notes").child(post_key).child("likecount"); mDatabaseLikeCount.keepSynced(true); mDatabaseLike.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (mProcessLike) { if (dataSnapshot.child(post_key).hasChild(auth.getCurrentUser().getUid())) { Log.i("D Diary", "User has already Liked. So it can be considered as Unliked."); mDatabaseLike.child(post_key).child(auth.getCurrentUser().getUid()).removeValue(); mDatabaseLikeCount.setValue(likeCount = likeCount - 1 ); mProcessLike = false; } else { Log.i("D Diary", "User Liked"); mDatabaseLike.child(post_key).child(auth.getCurrentUser().getUid()).setValue(auth.getCurrentUser().getDisplayName()); mDatabaseLikeCount.setValue(likeCount = likeCount + 1 ); Log.i(dataSnapshot.getKey(), dataSnapshot.getChildrenCount() + "Count"); mProcessLike = false; } } } @Override public void onCancelled(DatabaseError databaseError) { } }); } }); 
2
  • Can you show, how you define the likeCount variable? Commented Mar 13, 2017 at 9:04
  • in POJO it is initialized at 0 Commented Mar 13, 2017 at 12:39

1 Answer 1

1

This should work. You should use a transaction to increment or reduce a value that multiple people will be interacting with.

if (dataSnapshot.child(post_key).hasChild(auth.getCurrentUser().getUid())) { Log.i("D Diary", "User has already Liked. So it can be considered as Unliked."); mDatabaseLike.child(post_key).child(auth.getCurrentUser().getUid()).removeValue(); updateCounter(false); mProcessLike = false; } else { Log.i("D Diary", "User Liked"); mDatabaseLike.child(post_key).child(auth.getCurrentUser().getUid()).setValue(auth.getCurrentUser().getDisplayName()); updateCounter(true) Log.i(dataSnapshot.getKey(), dataSnapshot.getChildrenCount() + "Count"); mProcessLike = false; } 

With updateCounter:

private void updateCounter(bool increment) { mDatabaseLikeCount.runTransaction(new Transaction.Handler() { @Override public Transaction.Result doTransaction(MutableData mutableData) { if (mutableData.getValue() != null) { int value = mutableData.getValue(Integer.class); if(increment) { value++; } else { value--; } mutableData.setValue(value); } return Transaction.success(mutableData); } @Override public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) { // Transaction completed Log.d(TAG, "likeTransaction:onComplete:" + databaseError); } }); } 

Firebase Transactions

Sign up to request clarification or add additional context in comments.

6 Comments

can you please help me on where to put my update counter?
Just anywhere in the same class as the code you posted.
not its not working, if (value != null) it gives an error saying operator '!=' cannot be applied to 'int', 'null' and when i change from if (value != null) to if (value != 0) and run the app, counter is not updating anymore. even for one user
Try (mutableData.getValue() != null) and put the int value = mutableData.getValue(Integer.class); inside of your if statement, I'll edit it in the answer.
I'v realized that when i put if (value != null) to if (value != 0) and in my firebase likecount database i initialize counts from 0 to 1 it works perfect. problem now is that count needs to start from 0 not 1. thank you this has been veryhelpful
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.