2
Good morning Developers! I’m having a hard time in a project that I can’t move forward, here’s the problem/scenario: We have a screen that brings information, a message, and on the screen there is the button to bookmark that message, this information is saved in the Firebase Realtimedatabase, in this scenario I need the following elements: when the user clicks on imLike save as liked or neglected depending on the current situation(is working), every time check if that message had been liked by that user before(as the messages are generated on the screen randomly this check if the user had liked before should be done with the same frequency - is working already), and last but not least a Textview show how many likes that message already has (this I still could not do).
The function below is called as soon as Activity is startada and brings the following results: if the current user has already given like in that message the favorite image receives the red coloring and Textview tbm.
private void verificarCurtida(String versiculo, String usuario) {
String cripto = CriptografiaBase64.criptografarVersiculo(versiculo);
likeRef = ConfiguracaoFirebase.getFirebaseReference()
.child("VERSICULOS")
.child(cripto)
.child("LIKES")
.child(usuario);
//Verificando se esse versiculo já foi curtido antes
likeRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Pesquisando o idUsuarioe as curtidas
if (dataSnapshot.getValue() != null){
//Se retornar valor quer dizer que já fi curtida antes por esse usuário
imLike.setImageResource(R.drawable.ic_favorite);
tvLikes.setTextColor(Color.parseColor("#FF1E1E"));
} else {
imLike.setImageResource(R.drawable.ic_favorite_border);
tvLikes.setTextColor(Color.parseColor("#000000"));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
below is the same method, but with the attempt of adding the user count in the knot:
private void verificarCurtida2(String versiculo, String usuario) {
String cripto = CriptografiaBase64.criptografarVersiculo(versiculo);
final int contar = 0;
likeRef = ConfiguracaoFirebase.getFirebaseReference()
.child("VERSICULOS")
.child(cripto)
.child("LIKES");
//.child(usuario);
//Verificando se esse versiculo já foi curtido antes
likeRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Pesquisando o idUsuarioe as curtidas
if (dataSnapshot.getValue() != null){
for (DataSnapshot data: dataSnapshot.getChildren()){
if (data.getValue().equals(preferencias.getCHAVE_ID())){
//Se retornar valor quer dizer que já fi curtida antes por esse usuário
imLike.setImageResource(R.drawable.ic_favorite);
tvLikes.setTextColor(Color.parseColor("#FF1E1E"));
contar++;
} else {
imLike.setImageResource(R.drawable.ic_favorite_border);
tvLikes.setTextColor(Color.parseColor("#000000"));
contar++;
}
}
//tvLikes.setText((int) dataSnapshot.getChildrenCount());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
tvLikes.setText(contar);
}
Below, follows the structure in firebase, where I have the following nodes: VERSICULOS > IDVERSICULO > LIKES > ID’s of the users who clicked on like;
as image I need the following result: VERSICULOS > IDVERSICULO > LIKES > 3 (Textview gets 3).
//tvLikes.setText((int) dataSnapshot.getChildrenCount())
what happens here?– Lennoard Silva