2017-12-03 122 views
0

目前,我正在爲幾個不同的類實現一個名爲RowType的接口,讓我們假設類A,B和C.如何遍歷Firebase以獲取每個不同的對象?使用與Firebase的接口

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

我會假設,因爲插在火力地堡的所有對象要麼是A級,B,或C,我可以使用ROWTYPE來獲取快照值。但是,我收到一條錯誤消息,指出「在類RowType上找不到要序列化的屬性」。

UPDATE

接口

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

運動類

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; 
} 

} 

我有在接口實現其他類,但要保持這一簡短,我只增加鍛鍊班。其他班級比較相似。

+0

看看這個線程https://stackoverflow.com/questions/37743661/firebase-no-properties-to-serialize-found-on-class –

+0

請添加您的類/接口代碼。 –

+0

@AlexMamo我更新了我的代碼 –

回答

0

您不能用作模型類,接口。這是不可能的,因爲模型類必須具有Firebase所需的public no-argument constructor。正如我們所知,接口不能使用關鍵字new進行初始化,因此不能有構造函數。

爲什麼Firebase需要無參數構造函數?當Firebase實時數據庫SDK對來自數據庫的對象進行反序列化時,它需要使用的任何對象都有它可以用來實例化對象的對象,這對於一個接口來說是不可能的,因爲SDK並不知道如何創建它的一個實例。

+0

那麼,我將如何檢查快照是否屬於Exercise類或其他類? –

+0

即使我插入了類的對象,snapshot.getValue()。getClass()。getName()將返回一個HashMap。練習 –

+0

這是正確的,因爲Firebase中的每個節點都是一個Map。你沒有得到'Exercise'類的相應對象,因爲你指向了一個錯誤的'DatabaseReference'。你需要在你的'Exercise'類映射的確切位置上附加一個監聽器。你認爲我的回答對你有幫助嗎? –