创建模型类并在子节点中获取数据 [英] Create model class and get the data inside the child node
问题描述
我正在创建一个模型类以将其用于firebaserecycleradapter,但是我的问题是用户数据中有一个子节点.看起来像这样
I am creating a model class to use it to firebaserecycleradapter but my problem is there is a child node inside a user data. It looks like this
我需要获得以下信息:(名称,缩略图,年份,课程).这是我所做的
I need get the following: (name, thumbnail, year, course). This what I have done
public class Users {
private String name;
private String thumbnail;
//TODO: need to get the year and course, inside the Student
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
}
我如何获得学生的学年和课程
How can I get the year and course inside the student
推荐答案
要在用户数据中获取子节点数据,即Student(年,课程)中的值,您需要制作如下模型类:
To get the child node data inside a user data That is values inside Student (year, course) you need to make model class like below:
public class User {
private String name;
private String thumbnail;
private Student student;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public class Student {
private String year;
private String course;
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
}
}
您可以使用构造函数以及getter/setter方法.
You can use constructor as well as getter/setter method for the same.
要向模型中添加数据,请创建对象并使用如下所示的set方法:
For adding data to model, create object and use set method like below:
//Setting Data
User userObj = new User();
userObj.setName("XYZ");
userObj.setThumbnail("URL");
Student student = new User().new Student();
student.setYear("2018");
student.setCourse("MCA");
userObj.setStudent(student);
您只需要使用get方法将数据分配给模型类的对象并获取数据,就像这样...
You simply need to assign data to object of model class and the fetch data by using get methods, like this...
//Getting Data
String name=userObj.getName();
String thumbnail=userObj.getThumbnail();
String year=userObj.getStudent().getYear();
String course=userObj.getStudent().getCourse();
这篇关于创建模型类并在子节点中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!