第一个模型是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
正确显示它们?
提前致谢!