When making a query in firebase in a node like in this case the document, I want to bring two fields from that node and put them in a String variable, to add information to the database I don't use login, I use the push() method.
When pressing the consult button, the document is compared with the one entered in an edittext, then it would retrieve the value of the a1a2_a1 and a1a2_a2 fields , 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 entire 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) {
}
});
}
});
}
}
To make it more useful to you, we will 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 you have in firebase
then we just loop through each push key inside dick and get what we want ( what we defined in our bean )
in this case I am going to refer to cock
where mDatabase is
I hope it helps you