Skip to content Skip to sidebar Skip to footer

Convert Datasnapshot To Class Object

I'm newbie to Firebase Database and I'm following Google's Docs to retrive data from my database. I have this structure: root/ | |--- 82JWZZZd*** | | | |--- profile |

Solution 1:

If you have private fields, you need to add getters and setters. If you don't want setters and getters, use public fields.

Additionally, since your Profile class is nested inside your fragment, you'll need to mark it as static. So public static class Profile{. Also see this comment on Github: https://github.com/firebase/FirebaseUI-Android/issues/46#issuecomment-167373575

Solution 2:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference(getUid() + "/profile");
reference.addListenerForSingleValueEvent(newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()){
            //Create new collection<Profile> person;

            person=newArrayList<Profile>();
            Toast.makeText(getContext(), "" + dataSnapshot.getValue(),
                    Toast.LENGTH_LONG).show();
            Map<String,String> td=(HashMap<String, String>)dataSnapshot.getValue();
            Profile prf =newProfile(td.get("id"),td.get("name"),td.get("age"))

            person.add(prf);

        }
    }
    @OverridepublicvoidonCancelled(DatabaseError databaseError) {

    }
});

Post a Comment for "Convert Datasnapshot To Class Object"