Know a key and value with For iterator (DataSnapshot), Firebase - Android Studio

1

I have a problem with the for iterator (DataSnapshot) since in my app I have an EditText in which when you enter a number it is compared with all the "key" in the database Firebase and if that number entered is equal to some "key" the "value" of it is shown in a TextView.

At the moment I have only 5 "keys" with their respective "values" but my problem is that when I consult the value of key 1, 2, 3 or 4 it tells me that it does not exist (by the else statement of my code), however, if I check the value of the key 5 it tells me the corresponding value, in this case "Text 5 ".

This is the structure of the database:

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);
    final String TAG = "MainActivity";

    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) {

                    for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                            Log.d(TAG, "Key: " + postSnapshot.getKey());
                            Log.d(TAG, "Value: " + postSnapshot.getValue());

                        if (postSnapshot.getKey().equals(buscar)){
                           mEstado.setText("El numero #" + buscar + " tiene como texto: " + postSnapshot.getValue());

                        }else{
                            if (buscar.equals("")){
                                mEstado.setText("Ingrese un numero valido.");
                            }else {
                                mEstado.setText("El numero #" + buscar + " no existe.");
                            }
                        }
                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    });
}}

I think the problem is that is only reading the last "stored" value. As you can see below in the Android Monitor if it shows me all the values but when running it on my device as I mentioned it says it does not exist (for the else statement that I have declared).

And this is when running it on my device:

An apology if it is something extensive, I wanted to raise my problem as clear as possible with photos and code. Thanks.

    
asked by Cesar Perez 03.11.2017 в 01:10
source

1 answer

1

Since you are reading all values, the loop should stop when the required condition is met.

You can do that with break in this case:

                for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                        Log.d(TAG, "Key: " + postSnapshot.getKey());
                        Log.d(TAG, "Value: " + postSnapshot.getValue());

                    if (postSnapshot.getKey().equals(buscar)){
                       mEstado.setText("El numero #" + buscar + " tiene como texto: " + postSnapshot.getValue());
                       break;
                    }else{
                        if (buscar.equals("")){
                            mEstado.setText("Ingrese un numero valido.");
                        }else {
                            mEstado.setText("El numero #" + buscar + " no existe.");
                        }
                    }
                }

By not stopping, I kept comparing until reaching the last value.

    
answered by 03.11.2017 / 02:04
source