Query several fields of a node in firebase

2

When doing a query in firebase in a node like in this case the document, I want to bring two fields of that node and put them in a variable String, to add information to the database I do not use login, I use the push method ( ).

when clicking on the button, the document is compared with the one typed in an edittext, then it would recover the value of the fields a1a2_a1 and a1a2_a2 , in this case I receive the null value.

The listener that executes the query and tries to retrieve the value of the fields: a1a2_a1 and a1a2_a2 . I get null.

consultar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String dat_2 = doc.getText().toString();
            //Consultando una referencia
            Query q=refDatos.orderByChild("doc").equalTo(dat_2);
            q.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    int cont=0;
                    for(DataSnapshot datasnapshot: dataSnapshot.getChildren()){
                        cont++;
                    }
                    if(cont==0){
                        Toast.makeText(PollaMundialista.this, "No se encontró información ", Toast.LENGTH_LONG).show();
                    }
                    if(cont>0){
                        String dato1 = dataSnapshot.child("a1a2_a1").getValue().toString();
                        String dato2 = dataSnapshot.child("a1a2_a2").getValue().toString();
                        Toast.makeText(PollaMundialista.this, " Dato: "+dato2, Toast.LENGTH_LONG).show();
                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    });

The whole class:

public class PollaMundialista extends AppCompatActivity {

private EditText nom,doc,email,phone,a1a2_a1,a1a2_a2;
ImageButton enviar;
Button consultar;

FirebaseDatabase database = FirebaseDatabase.getInstance();

DatabaseReference root = FirebaseDatabase.getInstance().getReference();
DatabaseReference refDatos = root.child("uniagust/polla");
DatabaseReference c1 = root.child("uniagust/polla/doc");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pollamundialista);

    enviar = (ImageButton) findViewById(R.id.enviar1);
    consultar = (Button) findViewById(R.id.consultar);
    //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);

    enviar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String dat_1 = nom.getText().toString();
            final String dat_2 = doc.getText().toString();
            final String dat_3 = email.getText().toString();
            final String dat_4 = phone.getText().toString();
            final String dat_5 = a1a2_a1.getText().toString();
            final String dat_6 = a1a2_a2.getText().toString();

            //Consultando una referencia
            Query q=refDatos.orderByChild("doc").equalTo(dat_2);
            q.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    int cont=0;
                    for(DataSnapshot datasnapshot: dataSnapshot.getChildren()){
                        cont++;
                    }
                    if(cont==0){
                        Polla_uniagust polla_obj = new Polla_uniagust(dat_1,dat_2,dat_3,dat_4,dat_5,dat_6);
                        uniagust.child(FirebaseReferences.REFERENCE_2).push().setValue(polla_obj);
                        Toast.makeText(PollaMundialista.this, "Se ha enviado su participación ", Toast.LENGTH_LONG).show();
                    }
                    if(cont>0){
                        Toast.makeText(PollaMundialista.this, "Este documento ya se encuentra registrado, si es un error.... ", Toast.LENGTH_LONG).show();

                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
    });

    consultar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String dat_2 = doc.getText().toString();
            //Consultando una referencia
            Query q=refDatos.orderByChild("doc").equalTo(dat_2);
            q.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    int cont=0;
                    for(DataSnapshot datasnapshot: dataSnapshot.getChildren()){
                        cont++;
                    }
                    if(cont==0){
                        Toast.makeText(PollaMundialista.this, "No se encontró información ", Toast.LENGTH_LONG).show();
                    }
                    if(cont>0){
                        String dato1 = dataSnapshot.child("a1a2_a1").getValue().toString();
                        String dato2 = dataSnapshot.child("a1a2_a2").getValue().toString();
                        Toast.makeText(PollaMundialista.this, " Dato: "+dato2, Toast.LENGTH_LONG).show();
                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    });

}

}

    
asked by Ivan Alfredo 13.06.2018 в 01:46
source

1 answer

2

To make it more useful for you, let's do the following

We create a separate class (a bean) with the data to request, note that you can expand it to the current data that you have in firebase

public class Polla {


    private String a1a2_a1;
    private String a1a2_a2;

    public Polla(){

    }

    public Polla(String a1a2_a1, String a1a2_a2) {
        this.a1a2_a1 = a1a2_a1;
        this.a1a2_a2 = a1a2_a2;
    }

    public String getA1a2_a1() {
        return a1a2_a1;
    }

    public void setA1a2_a1(String a1a2_a1) {
        this.a1a2_a1 = a1a2_a1;
    }

    public String getA1a2_a2() {
        return a1a2_a2;
    }

    public void setA1a2_a2(String a1a2_a2) {
        this.a1a2_a2 = a1a2_a2;
    }



}

Then we just go through each push key inside the dick and we get what we want (what we define in our bean)

in this case I will refer to cock

mDatabase.child("uniagust").child("polla").orderByChild("doc").equalTo(dat_2).addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {

    for(DataSnapshot snapShot : dataSnapshot.getChildren()){
    Polla polla = snapShot.getValue(Polla.class);
    //Obtenemos los valores que queres
     String a1a2_a1 = polla.getA1a2_a1();
     String a1a2_a2 = polla.getA1a2_a2();

      Log.e("Datos: " , "" + a1a2_a1 + "" + a1a2_a2);

       }


  }

  @Override
  public void onCancelled(DatabaseError databaseError) {
    System.out.println("The read failed: " + databaseError.getCode());
  }
});

Where mDatabase is

DatabaseReference mDatabase;

mDatabase = FirebaseDatabase.getInstance().getReference();

I hope you serve

    
answered by 13.06.2018 / 05:31
source