0

Currently I am implementing an interface called RowType for a handful of different classes let's say class A, B, and C. How would I iterate through Firebase to get each different object?

.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        RowType rowType = snapshot.getValue(RowType.class);
                        Log.d("awdjk", "qwjel");
                    }

I would assume that since all the objects inserted in Firebase are of either class A, B, or C, that I could use RowType to get the snapshot value. However, I am getting an error saying that "No properties to serialize found on class RowType".

UPDATE

Interface

public interface RowType {
int EXERCISE_ROW = 0;
int REST_ROW = 1;
int SUPERSET_ROW = 2;
int PYRAMIDSET_ROW = 3;
int BUTTON_ROW = 4;
}

Exercise class

public class ExerciseModel implements RowType {
public String exercise, sets, reps, view, minutes, seconds, time, weight, childCount;
private EditText weightEditText, setsEditText, repsEditText;
private AutoCompleteTextView exerciseEditText;
private ImageView dragHandle;
private ExerciseRowTextWatcher exerciseRowTextWatcher;


public ExerciseRowTextWatcher getExerciseRowTextWatcher(){
    return exerciseRowTextWatcher;
}

public void setExerciseRowTextWatcher(ExerciseRowTextWatcher exerciseRowTextWatcher){
    this.exerciseRowTextWatcher = exerciseRowTextWatcher;
}


public ImageView getDragHandle(){
    return dragHandle;
}

public void setDragHandle(ImageView dragHandle){
    this.dragHandle = dragHandle;
}

public AutoCompleteTextView getExerciseEditText(){
    return exerciseEditText;
}

public void setExerciseEditText(AutoCompleteTextView exerciseEditText){
    this.exerciseEditText = exerciseEditText;
}

public EditText getWeightEditText(){
    return weightEditText;
}

public void setWeightEditText(EditText weightEditText){
    this.weightEditText = weightEditText;
}

public EditText getSetsEditText(){
    return setsEditText;
}

public void setSetsEditText(EditText setsEditText){
    this.setsEditText = setsEditText;
}

public EditText getRepsEditText(){
    return repsEditText;
}

public void setRepsEditText(EditText repsEditText){
    this.repsEditText = repsEditText;
}

public String getChildCount(){
    return childCount;
}

public void setChildCount(String childCount){
    this.childCount = childCount;
}

public String getExercise(){
    return exercise;
}

public String getWeight(){
    return weight;
}

public String getSets(){
    return sets;
}

public String getReps(){
    return reps;
}

public String getView(){
    return view;
}

public String getMinutes(){
    return minutes;
}

public String getSeconds(){
    return seconds;
}

public String getTime(){   return time;}

public void setExercise(String exercise){
    this.exercise = exercise;
}

public void setTime(String time){ this.time = time; }

public void setWeight(String weight){
    this.weight = weight;
}

public void setMinutes(String minutes){
    this.minutes = minutes;
}

public void setSeconds(String seconds){
    this.seconds = seconds;
}

public void setSets(String sets){
    this.sets = sets;
}

public void setView(String view){
    this.view = view;
}

public void setReps(String reps){
    this.reps = reps;
}

}

I have other classes that are implemented in the interface, but to keep this short, I have added only the exercise class. The other classes are relatively similar.

Gerald Tan
  • 91
  • 14

1 Answers1

1

You cannot use as a model class, an interface. This is not possible because the model class must have the public no-argument constructor which is needed for Firebase. As we know, an interface cannot be initialized using the new keyword and therefore cannot have a constructor.

Why does Firebase need the no-argument constructor? When the Firebase Realtime Database SDK deserializes objects coming from the database, it requires that any objects in use have a public no-argument constructor that it can use to instantiate the object and this is not possible with an interface because the SDK doesn't really know how to create an instance of it.

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • So how would I check to see if the snapshot is of class Exercise, or another class? – Gerald Tan Dec 05 '17 at 23:34
  • snapshot.getValue().getClass().getName() returns a HashMap even though I inserted an object of class Exercise – Gerald Tan Dec 05 '17 at 23:56
  • That's correct because every node in Firebase is a `Map`. You are not getting the corresponding object of `Exercise` class because you are pointing on a wrong `DatabaseReference`. You need to attach a listener on the exact location where your `Exercise` class is mapped. Do you think that my answer helped you? – Alex Mamo Dec 06 '17 at 08:32