Unable to read data from firebase in android

后端 未结 2 1314
醉梦人生
醉梦人生 2021-01-27 07:57

I am working on android app where Firebase is being used for maintaining mobile back end. I have database in this format on Firebase

{
         


        
相关标签:
2条回答
  • 2021-01-27 08:14

    Are you mapping the dataSnapshot to the correct java class?.

    Because I see you have named your file as Users.java and when you getValue, you create object for User.

                User user = dataSnapshot.getValue(User.class);
    

    Instead should it be

    Users user = dataSnapshot.getValue(Users.class);
    

    Just curious whether you have named it right.

    And also use a Map instead of a List for the Users and it will work for sure.

    `

    0 讨论(0)
  • 2021-01-27 08:23

    In order to display the data, you need change your code like this:

    mDatabase.child("users").child(userId).addValueEventListener(new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 
            String firstName = dataSnapshot.child("firstName").getValue(String.class); 
            Log.d("Scheduled", firstName); 
        } 
    
        @Override 
        public void onCancelled(DatabaseError error) { 
        // Failed to read value 
        } 
    });   
    

    In which userId is the unique id generated by the push() method.

    0 讨论(0)
提交回复
热议问题