I have a favouriting function where I store a user's favourite animals in firebase as a favourite array under their user ID. You can either favourite an animal from the recyclerview list or if you click on an animal from the list, it will go to a detailed page where you can favourite the animal there. This works great for favouriting and unfavouriting except for when you unfavourite the user's last animal causing the favourite list to be null. On the firebase end everything is fine but when you click back to go to the list, the UI still shows it as favourited even though it's not.
Setting the favourited in the detailed activity
if (user != null) {
//first check if favourited
if (favouritesList != null && favouritesList.contains(animalModel.getId())) {
btnFavourite.setImageResource(R.drawable.ic_favorite_white_36dp);
} else {
btnFavourite.setImageResource(R.drawable.ic_favorite_border_white_36dp);
}
btnFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//if favourited, unfavourite
if (favouritesList != null && favouritesList.contains(animalModel.getId())) {
btnFavourite.setImageResource(R.drawable.ic_favorite_border_white_36dp);
favouritesList.remove(animalModel.getId());
reference.setValue(favouritesList);
} else {
btnFavourite.setImageResource(R.drawable.ic_favorite_white_36dp);
if (favouritesList == null) { //if favourites list is null
favouritesList = new ArrayList<>();
favouritesList.add(animalModel.getId());
} else {
favouritesList.add(animalModel.getId());
}
reference.setValue(favouritesList);
}
}
});
}
Calling this getFavourites() method in the fragment hosting the recyclerview onCreate:
public void getFavourites(){
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null){
String userUid = user.getUid();
reference = FirebaseDatabase.getInstance().getReference(Common.USER).child(userUid).child(Common.FAVOURITE);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
GenericTypeIndicator<ArrayList<String>> t = new GenericTypeIndicator<ArrayList<String>>() {};
favouritesList = (ArrayList<String>)dataSnapshot.getValue(t);
if (favouritesList != null){
animalAdapter.setFavouritesList(favouritesList);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
The relevant adapter code:
@Override
public void onBindViewHolder(final ViewHolder holder, int position){
final AnimalType animalModel = mAnimalList.get(position);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
//if user logged in
if (user != null) {
//first check if favourited
if (favouritesList != null && favouritesList.contains(animalModel.getId())) {
holder.btnFavourite.setImageResource(R.drawable.ic_favorite_white_36dp);
} else {
holder.btnFavourite.setImageResource(R.drawable.ic_favorite_border_white_36dp);
}
String userUid = user.getUid();
reference = FirebaseDatabase.getInstance().getReference(Common.USER).child(userUid).child(Common.FAVOURITE);
holder.btnFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//if favourited, unfavourite
if (favouritesList != null && favouritesList.contains(animalModel.getId())){
holder.btnFavourite.setImageResource(R.drawable.ic_favorite_border_white_36dp);
favouritesList.remove(animalModel.getId());
reference.setValue(favouritesList);
}
else{
holder.btnFavourite.setImageResource(R.drawable.ic_favorite_white_36dp);
if(favouritesList == null){ //if favourites list is null
favouritesList = new ArrayList<>();
favouritesList.add(animalModel.getId());
}else{
favouritesList.add(animalModel.getId());
}
reference.setValue(favouritesList);
}
}
});
} else {
//if user not logged in
}
}
public void setFavouritesList(List<String> favouritesList){
this.favouritesList = favouritesList;
notifyDataSetChanged();
}
I found it. In the getFavourites() method it should be changed from
if (favouritesList != null){
animalAdapter.setFavouritesList(favouritesList);
}
to
animalAdapter.setFavouritesList(favouritesList);