Setter没有将值设置给Java对象。

huangapple go评论79阅读模式
英文:

Setter is not setting value to java object

问题

我正在尝试为我的 Java 对象 TaskModel 设置键Firebase 键)。但在使用 setter 后当我尝试使用 getter 检索数据时它返回 null

数据库结构

对于DataSnapshot dataSnapshot1: dataSnapshot.getChildren(){
    taskModel.setTaskID(dataSnapshot1.getKey());
    taskModel = dataSnapshot1.getValue(TaskModel.class);
    Log.d("myadaptor", taskModel.getTaskID());
    list.add(taskModel);
}

请帮助我将键设置到我的对象中

TaskModel.java

public class TaskModel {
    private String taskTitle;
    private String dueDate;
    private String taskDescription;
    private String taskID;

    public String getTaskID() {
        return taskID;
    }

    public void setTaskID(String taskID) {
        this.taskID = taskID;
    }

    public String getTaskTitle() {
        return taskTitle;
    }

    public void setTaskTitle(String taskTitle) {
        this.taskTitle = taskTitle;
    }

    public String getDueDate() {
        return dueDate;
    }

    public void setDueDate(String dueDate) {
        this.dueDate = dueDate;
    }

    public String getTaskDescription() {
        return taskDescription;
    }

    public void setTaskDescription(String taskDescription) {
        this.taskDescription = taskDescription;
    }

    public TaskModel(String taskTitle, String dueDate, String taskDescription) {
        this.taskTitle = taskTitle;
        this.dueDate = dueDate;
        this.taskDescription = taskDescription;
    }

    public TaskModel(){

    }
}
英文:

I'm trying to set key (firebase key) to my java object TaskModel. But after using setter when I'm trying to retrieve data using getter. It is returning null.

Database Structure

Setter没有将值设置给Java对象。

for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
taskModel.setTaskID(dataSnapshot1.getKey());
taskModel = dataSnapshot1.getValue(TaskModel.class);
Log.d("myadaptor", taskModel.getTaskID());
list.add(taskModel);
} 

Please help me set key to my object!

TaskModel.java

public class TaskModel {
private String taskTitle;
private String dueDate;
private String taskDescription;
private String taskID;
public String getTaskID() {
return taskID;
}
public void setTaskID(String taskID) {
this.taskID = taskID;
}
public String getTaskTitle() {
return taskTitle;
}
public void setTaskTitle(String taskTitle) {
this.taskTitle = taskTitle;
}
public String getDueDate() {
return dueDate;
}
public void setDueDate(String dueDate) {
this.dueDate = dueDate;
}
public String getTaskDescription() {
return taskDescription;
}
public void setTaskDescription(String taskDescription) {
this.taskDescription = taskDescription;
}
public TaskModel(String taskTitle, String dueDate, String taskDescription) {
this.taskTitle = taskTitle;
this.dueDate = dueDate;
this.taskDescription = taskDescription;
}
public TaskModel(){
}
}

答案1

得分: 0

在你的旧代码中,你将ID分配给了taskModel,之后你从dataSnapShot初始化了一个新的taskModel对象。而在dataSnapShot中,id的值为null。因此它类似于id被覆盖:

taskModel.setTaskID(dataSnapshot1.getKey());
taskModel = dataSnapshot1.getValue(TaskModel.class);

更新以下行的位置:

for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
    taskModel = dataSnapshot1.getValue(TaskModel.class);
    taskModel.setTaskID(dataSnapshot1.getKey());
    Log.d("myadaptor", taskModel.getTaskID());
    list.add(taskModel);
}
英文:

Here in your old code, you are assigning ID to taskModel, after that you initializing taskModel as new object from dataSnapShot. And in dataSnapShot you have null as id. SO it is like overridden of id:

            taskModel.setTaskID(dataSnapshot1.getKey());
taskModel = dataSnapshot1.getValue(TaskModel.class);

Update the position of line:

        for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
taskModel = dataSnapshot1.getValue(TaskModel.class);
taskModel.setTaskID(dataSnapshot1.getKey());
Log.d("myadaptor", taskModel.getTaskID());
list.add(taskModel);
} 

huangapple
  • 本文由 发表于 2020年4月5日 18:33:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/61041225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定