I want to implement a function of 'add to favorite' by clicking in a star (button). When i click for the first time, set a value to user favorite in firebase and the star will be yellow, and when i click again, it removes from list, and star back to normal. I'm tryin' this code, but is looping. How can i solve this?
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference( "Usuarios" );
ref.child( mAuth.getUid() ).child( "Favoritos" )
.addValueEventListener( new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
botaoFavorito.setImageResource( R.drawable.ic_favoritos );
ref.child( mAuth.getUid() ).child( "Favoritos" ).child( posicao ).setValue(null);
}
else {
botaoFavorito.setImageResource( R.drawable.ic_favorito_adicionado );
ref.child( mAuth.getUid() ).child( "Favoritos" ).child( posicao ).setValue(posicao);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
} );
question from:https://stackoverflow.com/questions/65844809/recycler-view-creates-infinity-posts-when-i-tap-the-like-button