I'm doing an app where entering a EditText
a value, if it matches one of the database in Firebase will show in TextView
what the value of that field.
Below I show you a picture of how the database is structured:
To understand better I mean that in the EditText
enter a number and if it matches one of the numbers in the database will show me the "Text n" corresponding.
And this is the code
public class MainActivity extends AppCompatActivity {
private DatabaseReference mDatabase;
private TextView mEstado;
private Button B_buscar;
private EditText E_buscar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabase = FirebaseDatabase.getInstance().getReference().child("no_orden");
mEstado = (TextView) findViewById(R.id.estado);
B_buscar = (Button) findViewById(R.id.B_buscar);
E_buscar = (EditText) findViewById(R.id.E_buscar);
B_buscar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String buscar = E_buscar.getText().toString().trim();
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String estado = (String) dataSnapshot.getValue().toString();
if (estado.equals(dataSnapshot.child(buscar))) {
//Haz algo
mEstado.setText("El estado de la orden #" + buscar + " es: " + estado);
//Haz otra cosa
}else {
if (buscar.equals("")){
mEstado.setText("Ingrese un numero de orden valido.");
}else {
mEstado.setText("La orden #" + buscar + " no existe.");
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}}
The problem I have is that whenever I enter a number it tells me that this value does not exist, that is, it passes to the last else statement. I think he's not doing the comparison with the DB data. I hope you can help me, thank you.