I want to query if a certain value is found in the database. What I am trying to do is obtain the reference of the value to query and do the traversal and if it finds an equal value, it prints a toast, and counts how many times it found the value. It's not doing it here and I think that's how 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 looks like this:
Reference class:
public class FirebaseReferences {
final public static String REFERENCE_1 = "uniagust";
final public static String REFERENCE_2 = "polla";
}
To compare the value you should first change the name of the DataSnapshot of your for because it is different from that of the reference, since it goes through an array and returns values from it
Your doc value is of type string in firebase, you can see it because it has "" , at first what I do is save the value of the doc reference in a string, and then I compare each value of the snapshot that runs through the nodes with that string , if they are equal I increase the counter. I use equals since we compare
Strings