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(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()){
            //Create new collection<Profile> person;

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

            person.add(prf);

        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

Post a Comment for "Convert DataSnapshot To Class Object"