我调用了ListView中的数据:
private void displayStudentChatMessages() {
listofMessages = (ListView) findViewById(R.id.list_msg);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(String.valueOf(generateChatID));
adapter = new FirebaseListAdapter<StudentMessageModel>(this, StudentMessageModel.class, R.layout.item_chat_right, ref) {
@Override
protected void populateView(View v, StudentMessageModel model, int position) {
TextView messageUsername = (TextView) findViewById(R.id.message_user_sender);
TextView messageText = (TextView) findViewById(R.id.message_user);
TextView messageTime = (TextView) findViewById(R.id.message_usertime);
messageUsername.setText(model.getStudentName());
messageText.setText(model.getStudentMessage());
messageTime.setText(DateFormat.format("yyyy-MM-dd (HH:mm:ss)", model.getStudentTime()));
}
};
listofMessages.setAdapter(adapter);
listofMessages.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
}
但是我得到了错误:
com.google.firebase.database.DatabaseException:类com.androidports.autosiskola.StudentMessageModel缺少没有参数的构造函数
我需要那些根目录的孩子是生成名称的数据( test-testteacher-student-teacher )。
以下是我添加构造函数的方法:
public class StudentMessageModel {
private String studentUsername;
private String studentName;
private String studentMessage;
private String generatedChatID;
private long studentTime;
public StudentMessageModel(String studentUsername, String studentName, String studentMessage, String generatedChatID) {
this.studentUsername = studentUsername;
this.studentName = studentName;
this.studentMessage = studentMessage;
this.generatedChatID = generatedChatID;
studentTime = new Date().getTime();
}
和getter and setters。
答案 0 :(得分:2)
我认为错误是不言自明的。您必须具有不带参数的构造函数。像这样:
public class StudentMessageModel {
private String studentUsername;
private String studentName;
private String studentMessage;
private String generatedChatID;
private long studentTime;
public StudentMessageModel(String studentUsername, String studentName, String studentMessage, String generatedChatID) {
this.studentUsername = studentUsername;
this.studentName = studentName;
this.studentMessage = studentMessage;
this.generatedChatID = generatedChatID;
studentTime = new Date().getTime();
}
public StudentMessageModel(){}
答案 1 :(得分:2)
正如错误消息所示,您必须拥有不带参数的公共构造函数。从数据库重新创建对象时使用此方法。
此外,您必须将字段标记为公共字段,或者为它们提供getter和setter。这样Firebase就可以知道从数据库中的JSON属性填充哪些字段。
所以:
public class StudentMessageModel {
public String studentUsername;
public String studentName;
public String studentMessage;
public String generatedChatID;
public long studentTime;
public StudentMessageModel(String studentUsername, String studentName, String studentMessage, String generatedChatID) {
this.studentUsername = studentUsername;
this.studentName = studentName;
this.studentMessage = studentMessage;
this.generatedChatID = generatedChatID;
studentTime = new Date().getTime();
}
public StudentMessageModel() {
// public default constructor, which Firebase uses
}
答案 2 :(得分:-1)
将您的字段公开添加,也许获取器和设置器会有所帮助!