I'm a beginner in android programming, and I needed to develop a voting system with several categories, where the user can register to participate, in the system can only give one vote a day, how did I do it? to using firebase since I can not link , I did a gambiarra , every time you vote is stored the vote date in String as in the image in this case of the female cosplay Anyone who voted your id and your information will go to cosplay_fem_TIME
If the user is able to vote then the participant's profile is added +1 votes as in the image the problem that sometimes these votes are decreasing and a lot, sometimes the participant even loses 30 points, and is at once
Follow my code.
To get the result of the participant's votes updated
private void CarregarDados_Categoria_Selecionada(String id){
ChildEventListenecategoria=mDatabasecategoria.orderByChild("id").equalTo(id).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Categoria_cosplay_fem categoria=dataSnapshot.getValue(Categoria_cosplay_fem.class);
QntdVotos=categoria.getVotos();
Log.i("sdsd2", String.valueOf(categoria.getVotos()));
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Categoria_cosplay_fem categoria=dataSnapshot.getValue(Categoria_cosplay_fem.class);
QntdVotos=categoria.getVotos();
Log.i("sdsd2", String.valueOf(categoria.getVotos()));
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
This is my code that compares the date entered in string with the current date also in string if it is different it excludes the user id of cosplay_fem_TIME, so the user can vote again
private void Verificar_Tempo_votacao(){
final Calendar calendartempo = Calendar.getInstance();
final SimpleDateFormat simpleDateFormats = new SimpleDateFormat("yyyy-MM-dd",java.util.Locale.getDefault());// MM'/'dd'/'y;
database_tempo.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Categoria_Tempo_cosplay_fem_time tempo = dataSnapshot.getValue(Categoria_Tempo_cosplay_fem_time.class);
String DataAtual = simpleDateFormats.format(calendartempo.getTime());
Log.i("sdsds", DataAtual + "datacadastrada" + tempo.getTempodata());
if (!DataAtual.equals(tempo.getTempodata())) {
database_tempo.removeValue();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
When the user clicks on the VOTE button he calls the class Time (); that goes in cosplay_fem_TIME check if it has the user id
private void Tempo(){
database_tempo.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.exists()){
ValidarVoto();
}else{
Toast toast = Toast.makeText(Detalhe_cosplay_fem.this, " Seu voto nessa categoria já foi registrado,volte amanhã.",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast.show(); }
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
If it does not then it validates and saves the +1 vote
private void ValidarVoto() {
Toast toast = Toast.makeText(Detalhe_cosplay_fem.this, "Carregando...",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
//tempo
final Calendar calendartempo = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd",java.util.Locale.getDefault());// MM'/'dd'/'y;
long millis = new Date().getTime();
Categoria_Tempo_cosplay_fem_time tempo = new Categoria_Tempo_cosplay_fem_time();
String DataDoVoto = simpleDateFormat.format(calendartempo.getTime());
tempo.setId_categoria(categoriaselecionado.getId());
tempo.setId_usuario(identificadorUsuario);
tempo.setTempodata(DataDoVoto);
tempo.setTempo_milisigundos(millis);
tempo.SalvarTempo();
//categoria
int qtdVotos=QntdVotos+1;
HashMap<String,Object> dados = new HashMap<>();
dados.put("votos",qtdVotos);
mDatabasecategoria.child(categoriaselecionado.getId()).updateChildren(dados).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast toast = Toast.makeText(Detalhe_cosplay_fem.this, "Voto confirmado com sucesso!",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
Intent it = new Intent(Detalhe_cosplay_fem.this, Resultado_cosplay_fem.class);
startActivity(it);
finish();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast toast = Toast.makeText(Detalhe_cosplay_fem.this, "Ocorreu um erro, tente novamente ou verifique sua conexão"
,Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
});
}
I hope I made it clear, I know that this code is not in the best way but it was the best I could do so far, and I can not solve that loss of the participant's points of votes, when I tested with a maximum of 2 users voting at the same time everything went fine, but I do not know if it is because there is more than 10 users voting in the same participant and there is a bug, any help is welcome. Thanks