英文:
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
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论