Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get value from list of objects Firebase Android Studio

I have the following code:

    public void signOn(String id, String password){
        usersReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                boolean valid = false;
                for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                    String key = singleSnapshot.getKey();
                    if (key.equals(id)) {
                        singleSnapshot.getValue()//THIS LINE THIS LINE
                        Intent myIntent = new Intent(WelcomePage.this, MainActivity.class);
                        myIntent.putExtra("key", key);
                        WelcomePage.this.startActivity(myIntent);
                    }
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });

    }

When I call singleSnapshot.getValue(), I get {password=passwordExample, username=usernameExample} as those are the keys and their values in my realtime database. How can I access the password value and the username value? Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

One way to get it as HashMap and extract value from it, like this.

Map<String, String> map = (Map<String, String>) singleSnapshot.getValue();
String username = (String) map.get("username"); // You should pass exact key names here.
String password = (String) map.get("password");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading