• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

使用与Firebase的接口

Android 来源:Gerald Tan 4次浏览

目前,我正在为几个不同的类实现一个名为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; 
} 

} 

我有在接口实现其他类,但要保持这一简短,我只增加锻炼班。其他班级比较相似。


===========解决方案如下:

您不能用作模型类,接口。这是不可能的,因为模型类必须具有Firebase所需的public no-argument constructor。正如我们所知,接口不能使用关键字new进行初始化,因此不能有构造函数。

为什么Firebase需要无参数构造函数?当Firebase实时数据库SDK对来自数据库的对象进行反序列化时,它需要使用的任何对象都有它可以用来实例化对象的对象,这对于一个接口来说是不可能的,因为SDK并不知道如何创建它的一个实例。


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)