DeluxeD
DeluxeD

Reputation: 143

FirebaseDatabase: Model is missing a constructor with no arguments

So I've added Firebase data like this: Database

and I called the data inside a 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);
}

But I got the error:

com.google.firebase.database.DatabaseException: Class com.androidports.autosiskola.StudentMessageModel is missing a constructor with no arguments

I need those data where the root's child is the generated-name (test-testteacher-student-teacher).

Here is how I added the constructor:

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

and getters and setters.

Upvotes: 0

Views: 815

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599216

As the error message says, you must have a public constructor without arguments. This is used when re-creating your object from the database.

In addition you must either mark your fields as public, or provide getters and setters for them. That way Firebase knows what fields to populate from the JSON properties in the database.

So:

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
    }

Upvotes: 2

Ros&#225;rio P. Fernandes
Ros&#225;rio P. Fernandes

Reputation: 11326

I believe the error is self-explanatory. You must have a constructor with no arguments. Like this:

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(){}

Upvotes: 2

Related Questions