I want to check if a certain value is in the database. What I'm trying to do is get the value reference to consult and do the tour and if it finds an equal value, I print a toast, and count how many times it found the value. Here he is not doing it and I think it's the way I get the reference.
The references to the database are in a separate class, for this case I would like to consult in this class the reference to the document field "doc".
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pollamundialista);
enviar = (ImageButton) findViewById(R.id.enviar1);
//Datos personales
nom = (EditText) findViewById(R.id.nom);
doc = (EditText) findViewById(R.id.doc);
email = (EditText) findViewById(R.id.email);
phone = (EditText) findViewById(R.id.phone);
//Predicciones
a1a2_a1 = (EditText) findViewById(R.id.a1a2_a1);
a1a2_a2 = (EditText) findViewById(R.id.a1a2_a2);
final DatabaseReference uniagust = database.getReference(FirebaseReferences.REFERENCE_1);
final DatabaseReference polla = database.getReference(FirebaseReferences.REFERENCE_2);
//Consulta
//bbdd = FirebaseDatabase.getInstance().getReference(FirebaseReferences.REFERENCE_2);
enviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Consultando una referencia
Query q=uniagust.orderByChild("doc").equalTo("963258");
q.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int cont=0;
for(DataSnapshot datasnapshot: dataSnapshot.getChildren()){
cont++;
Toast.makeText(PollaMundialista.this, "Encontrado "+cont, Toast.LENGTH_LONG).show();
}
if(cont<0){
Toast.makeText(PollaMundialista.this, "El valor no se encuentra registrado"+cont, Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
The firebase database is displayed like this:
References class:
public class FirebaseReferences {
final public static String REFERENCE_1 = "uniagust";
final public static String REFERENCE_2 = "polla";
}