Getting the value of a field in Firebase in Android Studio

0

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.

    
asked by Cesar Perez 02.11.2017 в 18:04
source

1 answer

1

Your method onDataChange should be more or less like that.

  • The data is read in a for loop.
  • This line: ds.getValue().equals(buscar) && !ds.getKey().equals("1") evaluates if the value of each key is equal to the value of buscar and at the same time evaluates that the key is not equal to 1 .

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    if (ds.getValue().equals(buscar) && !ds.getKey().equals("1")){
    
                        //Iguales
    
                        }else{
    
                            //No iguales
    
                        }
                }
            }
    
answered by 02.11.2017 в 22:35