如何在Android的Firebase数据库中使用模型之间的关系?

时间:2017-02-23 10:01:16

标签: android firebase firebase-realtime-database firebaseui

我有一个非常简单的Firebase数据库,如下所示: enter image description here

第一个模型是ProfessorModel,如下所示:

public class ProfessorModel {
private String professorId;
private String professorName;

public ProfessorModel() {}

public ProfessorModel(String professorId, String professorName, Courses courses) {
    this.professorId = professorId;
    this.professorName = professorName;
    this.courses = courses;
}

public void setProfessorId(String professorId) {this.professorId = professorId;}
public String getProfessorId() {return professorId;}

public void setProfessorName(String professorName) {this.professorName = professorName;}
public String getProfessorName() {return professorName;}

public void setCourses(Courses courses) {this.courses = courses;}
public Courses getCourses() {return courses;}

public static class Courses {
    private String courseName;

    public Courses() {}

    public Courses(String courseName) {this.courseName = courseName;}

    public void setCourseName(String courseName) {this.courseName = courseName;}
    public String getCourseName() {return courseName;}
}

第二个是CourseModel,看起来像这样:

public class CourseModel {
private String courseName;

public CourseModel() {}

public CourseModel(String courseName) {
this.courseName = courseName;
}

public void setCourseName(String courseName) {this.courseName = courseName;}
public String getCourseName() {return courseName;}

这是我添加course

的方法
CourseModel cm = new CourseModel(courseName);
DatabaseReference courseRef = ref.push();
String key = courseRef.getKey();
courseRef.setValue(cm);

professorsRef.child(professorId).child("courses").child(key).setValue(courseName);

当我尝试使用此代码显示来自Course1的{​​{1}}和Course2时:

ProfessorModel

我收到此错误: DatabaseReference coursesRef = professorsRef.child(professorId).child("courses"); FirebaseListAdapter adapter = new FirebaseListAdapter<ProfessorModel>(this, ProfessorModel.class, android.R.layout.simple_list_item_1, coursesRef) { @Override protected void populateView(final View v, ProfessorModel professorModel, int position) { String courseName = professorModel.getCourses().getCourseName(); ((TextView) v.findViewById(android.R.id.text1)).setText(courseName); } }; ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); 。如何获取Can't convert object of type java.lang.String to type com.firebasedatabase.Models.ProfessorModel并使用course names正确显示它们?

提前致谢!

0 个答案:

没有答案