-1

I can't seem to figure out how to read the data from datasnapshot into my own class.

Here is the function where I attempt to do so:

    private void initializeFirebase(){
    my_db = FirebaseDatabase.getInstance();
    DatabaseReference my_ref = my_db.getReference();
    Map<String, ?> values = sharedPreferences.getAll();

    if (!values.isEmpty()){
        final String id = Profile.getCurrentProfile().getId();
        my_ref = my_ref.child("users").child("userid").child(id);
        if (my_ref != null) my_ref.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                value = dataSnapshot.getValue(UserData.class);
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

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

            }
        });
        mergeClassToSharedPreferences();
    }
    else {
        for (Map.Entry<String, ?> entry : values.entrySet()) {
            my_ref.child("userid").child(Profile.getCurrentProfile().
                    getId()).child(entry.getKey()).setValue(entry);
        }
    }
}

And here is the class I wrote:

 public class UserData {

    public String age, customized_message, dreams_response, gender, intro_response,
            rpp_phone_number, substances_response;
    public boolean relapse_prevention_program, firstrun;
    public int distance;

    public UserData(){

    }

    public UserData(String age, String customized_message, String dreams_response, String gender,
                    String intro_response, String rpp_phone_number, String substances_response,
                    boolean relapse_prevention_program, boolean firstrun, int distance){

        this.age = age;
        this.customized_message = customized_message;
        this.dreams_response = dreams_response;
        this.gender = gender;
        this.intro_response = intro_response;
        this.rpp_phone_number = rpp_phone_number;
        this.substances_response = substances_response;
        this.relapse_prevention_program = relapse_prevention_program;
        this.firstrun = firstrun;
        this.distance = distance;
    }

    public String getAge(){
        return age;
    }
    public String getCustomized_message(){
        return customized_message;
    }
    public String getDreams_response(){
        return dreams_response;
    }
    public String getGender(){
        return gender;
    }
    public String getIntro_response(){
        return intro_response;
    }
    public String getRpp_phone_number(){
        return rpp_phone_number;
    }
    public String getSubstances_response(){
        return substances_response;
    }
    public boolean getRelapse_prevention_program(){
        return relapse_prevention_program;
    }
    public boolean getFirstrun(){
        return firstrun;
    }
    public int getDistance(){
        return distance;
    }

    public void setDreams_response(String dreams_response){
        this.dreams_response = dreams_response;
    }
    public void setGender(String gender){
        this.gender = gender;
    }

    public void setIntro_response(String intro_response){
        this.intro_response = intro_response;
    }
    public void setRpp_phone_number(String rpp_phone_number){
        this.rpp_phone_number = rpp_phone_number;
    }
    public void setAge(String age){
        this.age = age;
    }
    public void setCustomized_message(String customized_message){
        this.customized_message = customized_message;
    }
    public void setSubstances_response(String substances_response){
        this.substances_response = substances_response;
    }
    public void setRelapse_prevention_program(boolean relapse_prevention_program){
        this.relapse_prevention_program = relapse_prevention_program;
    }
    public void setFirstrun(boolean firstrun){
        this.firstrun = firstrun;
    }
    public void setDistance(int distance){
        this.distance = distance;
    }
}

I believe I'm accessing my tree correctly, but every time I try and run this code it simply reports that "W/ClassMapper: No setter/field for key or value found on class". enter image description here

1 Answers1

0

The way you're modeling your data in Realtime Database is not compatible with the way that the SDK performs automatic serialization. It only accepts key/value pairs, where the keys map to the fields in your object, and the values match the type of data each field can contain. Your data is structured with objects for each field. If you use this structure, you'll need to write custom code to get all that data out of snapshots and into your POJO.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thank you so much! If I use setValue(UserData instance), passing in an instance of the class with all the variables initialized, will it automatically create the key-value pair structure so that I can redownload the data later into a new instance of the class? – Braedan Shigley Jul 09 '18 at 22:58
  • As long as the values in there are all simple types. – Doug Stevenson Jul 09 '18 at 23:19