Query a value in the firebase realtime database

1

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";
}
    
asked by Ivan Alfredo 05.06.2018 в 16:07
source

1 answer

1

To compare the value you should first change the name of the DataSnapshot of your for because it is different from the reference, since it traverses an array and returns values of the same

 Query q=uniagust.orderByChild("doc").equalTo("963258");
            q.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    int cont=0;
                    String doc = dataSnapshot.getValue(String.class);
                    for(DataSnapshot snapShot: dataSnapshot.getChildren()){
                        if(snapShot.equals(doc))
                        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();                            
                    }

                }

Your doc value is a string type 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 traverses the nodes with that string, if they are equal, increase the counter. I use equals since we compare Strings

    
answered by 05.06.2018 / 20:29
source