I am working on an application where I have a list of items on my home screen on every item there is a heart icon(button) if the user clicks on that icon(button) then the particular item is added to the wishlist for the user. Now the problem is that when the user clicks that button that item is adding to wishlist successfully but I want that the button should be turned red ONCE USER CLICKS IT AND that item is added to the wishlist.
CODE TO ADD THE ITEMS TO THE WISHLIST
holder.wishlist.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
int pos = holder.getBindingAdapterPosition();
//CODE TO ADD THE ITEM TO THE WISHLIST
if(compoundButton.isChecked())
{
firebaseFirestore.collection("wishlistDetails").document(uid).set(wishMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful())
{
Toast.makeText(context, "item added to wishlist" + pos , Toast.LENGTH_SHORT).show();
homeFragment.checkIdFavorite(position, holder);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
CODE TO TURN THE BUTTON RED
public void checkIfFavorite(int index, RecyclerviewAdapter.Viewholder viewholder) {
db.collection("wishlistDetails").document(datalist.get(index).getUid()).addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
isInFavrite = value.exists();
if (isInFavrite) {
viewholder.wishlist.setButtonDrawable(R.drawable.ic_favorite_filled);
} else {
viewholder.wishlist.setChecked(true);
}
}
});
}