Sharon
Sharon

Reputation: 3909

How do I convert Firebase data into a Java object?

I know this has been asked before in some forms, but all the answers I find seem to be retrieving a list of objects, not a single object, and I just seem to be going round in circles.

I'm using Android Studio, connecting to Firebase for the database. I have my own ChartColour Object defined in my app, as follows:

public class ChartColour {
    private String key, name, description;
    private int alpha, red, green, blue;

    public ChartColour(String thisKey) {
        this.key = thisKey;
    }

public ChartColour(String thisKey, String thisName, int thisAlpha, int thisRed, int thisGreen, int thisBlue) {
        this.key = thisKey;
        this.alpha = thisAlpha;
        this.red = thisRed;
        this.green = thisGreen;
        this.blue = thisBlue;
        this.description = null;
    }

    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("key", key);
        result.put("name", name);
        result.put("description", description);
        result.put("a", alpha);
        result.put("r", red);
        result.put("g", green);
        result.put("b", blue);

        return result;
    }

}

In the database, a sample record is like this:

colours
-- 5ha8hkwe253 (unique key for this chart)
     -- a:0
     -- b:255
     -- description: "Base colour"
     -- g:255
     -- name: "Base"
     -- r: 255

So, how do I retrieve this and set up a ChartColour, if I know the key? So far I have this function defined in my ColourChart.java class:

private void retrieveColourFromDatabase() {

    DatabaseReference mColourReference = FirebaseDatabase.getInstance().getReference()
        .child("colours").child(this.key);

    mColourReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
                ChartColour colour = userSnapshot.getValue(ChartColour.class);
            }
        }

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

However, that sets it all up into the 'colour' variable, right? But I want it to be accessible via "this". But if I do:

this = userSnapshot.getValue(ChartColour.class);

in the for loop, it takes 'this' as the ValueEventListener, not the ChartColour. I know this is probably really simple, but I just can't get my head round it, and I think I've gone round in circles so much that I've confused myself! I'm relatively new to Java, which isn't helping.

Upvotes: 1

Views: 683

Answers (1)

Dimitar
Dimitar

Reputation: 4783

Your ChartColour doesn't meet the requirements for marshaling the data into class. Your class has to fulfill these 2 properties:

  1. The class must have a default constructor that takes no arguments.
  2. The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized.

In short, add public ChartColour() {}; to your class and a Getter per each parameter of your non-default constructor. Then call

ChartColour colour = userSnapshot.getValue(ChartColour.class);

If you want to use this, change it to ChartColour.this, assuming that is your outer class.

Upvotes: 1

Related Questions