0

the databas

setContentView(R.layout.activity_main);
        Button readBtn = (Button) findViewById(R.id.readBtn);
        EditText testInputTxt = (EditText) findViewById(R.id.testInputTxt);
        
        rootRef = FirebaseDatabase.getInstance().getReference().child("Login");
        Query query = rootRef.orderByChild("LoginUserName").equalTo(testInputTxt.getText().toString());



        readBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                query.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {

                        String passwordUser = snapshot.child("LoginPassword").getValue(String.class);
                        testInputTxt.setText(passwordUser+"tatata");

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });
            }
        });

the query works fine it detects that the user is existexists but i wanted to retrieve the value of LoginPassword but it returns null i tried the for DataBase....:snapshot code but it won't detect

1 Answers1

0

Since you are using a Query object, it means that you can potentially get multiple results from the database. That being said, you have to loop through the results using a call to .getChildren() as seen in the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference loginRef = rootRef.child("Login");
Query query = loginRef.orderByChild("LoginUserName").equalTo(testInputTxt.getText().toString());
query.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String passwordUser = ds.child("LoginPassword").getValue(String.class);
                testInputTxt.setText(passwordUser+"tatata");
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

Please also note that if two users have the same "LoginUserName", then your TextView will always display the value of the last one. So always be sure to have unique user names. To check if a user name already exists, please check my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I would like to add question after i change the code the tag gave me this :" D/TAG: Client is offline" How can i fix this? – Christian Jake Managase Sep 10 '21 at 17:20
  • Most likely your client is offline. If you have a hard time solving it, please post a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Sep 11 '21 at 07:37