我只想从我的 firebase
数据库中检索数据,但我不知道获取该数据的正确方法。这是我在 android studio 中的错误:
W/ClassMapper: No setter/field for chapterTwo found on class
com.junburg.moon.rockbottom.model.Chapter
W/ClassMapper: No setter/field for chatperOne found on class
com.junburg.moon.rockbottom.model.Chapter
W/ClassMapper: No setter/field for chapterTwo found on class
com.junburg.moon.rockbottom.model.Chapter
W/ClassMapper: No setter/field for chapterOne found on class
com.junburg.moon.rockbottom.model.Chapter
这是我的数据类。这是一个 Chapter.class
模型。
public class Chapter {
private String chaterName;
private String chaterExplain;
public Chapter() {
}
public Chapter(String chaterName, String chaterExplain) {
this.chaterName = chaterName;
this.chaterExplain = chaterExplain;
}
public String getChaterName() {
return chaterName;
}
public void setChaterName(String chaterName) {
this.chaterName = chaterName;
}
public String getChaterExplain() {
return chaterExplain;
}
public void setChaterExplain(String chaterExplain) {
this.chaterExplain = chaterExplain;
}
}
和Study.class
模型
public class Subject {
private String subjectName;
private String subjectExplain;
private Chapter chapter;
public Subject() {
}
public Subject(String subjectName, String subjectExplain, Chapter
chapter) {
this.subjectName = subjectName;
this.subjectExplain = subjectExplain;
this.chapter = chapter;
}
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public String getSubjectExplain() {
return subjectExplain;
}
public void setSubjectExplain(String subjectExplain) {
this.subjectExplain = subjectExplain;
}
public Chapter getChapter() {
return chapter;
}
public void setChapter(Chapter chapter) {
this.chapter = chapter;
}
}
这是我的 addValueEventListener
代码:
private void getStudyData() {
databaseReference.child("study")
.child("subject")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
subjectList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Subject subject = ds.getValue(Subject.class);
subjectList.add(subject);
Log.d(TAG, "onDataChange: " +
subject.getChapter().getChaterName() +
subject.getChapter().getChaterExplain());
}
studyRecyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
最后,这是我在 Firebase 数据库中的 JSON 树。
我尝试了一些方法,但无法解决这个错误。
代码执行不返回章节类数据。例如:
subject.getChapter().getChaterName()
subject.getChapter().getChaterExplain()
非常感谢您帮助处理这件事。感谢您的关心。
最佳答案
Chapter
的 POJO 类中有几个错误。首先,错误消息清楚地表明缺少 chapterOne
和 chapterTwo
属性。您的 POJO 中需要它。
因此您的 Chapter.java
应该如下所示。
public class Chapter {
public ChapterOne chapterOne;
public ChapterTwo chapterTwo;
}
并且还需要定义ChapterOne
和ChapterTwo
类。
public class ChapterOne extends ChapterDetails {
}
public class ChapterTwo extends ChapterDetails {
}
public class ChapterDetails {
public String chapterName;
public String chapterExplain;
}
请注意,您在 Chapter
pojo 中拼错了 chapterName
和 chapterExplain
。当前的变量名称是 chaterName
和 chaterExplain
这是错误的。请同时修复变量名称。
更新
But can i get data to change variable type in chapter data of subject class? List.. Map.. or.. somethnig else like that. Not make new class ChapterOne.. ChapterTwo..
对于您当前的实现,这是不可能的。但是,我建议对您的 firebase 数据库实现稍作更改。让我们以 chapter
节点进行修改。
chapter
- chapterDetails
- chapterId : 1
- chapterName : Something one
- chapterExplain : Some explanation
- chapterDetails
- chapterId : 2
- chapterName : Something two
- chapterExplain : Some explanation
如果您像上面那样修改您的 firebase 数据库结构,那么您可能会想出以下 POJO 类。
public class Chapter {
List<ChapterDetails> chapterDetails;
}
public class ChapterDetails {
public Long chapterId;
public String chapterName;
public String chapterExplain;
}
I'm sorry I asked too much. But the same chapterDetail is not added to the Firebases. I tried to add a chapterDetail with chapterId 1 and a chapterDetail with chapterId 2, but it disappears or is replaced.
我刚刚展示了一个伪实现。抱歉,如果更新的答案具有误导性。但是,请使用以下方法在 firebase 数据库中设置您的数据。这样您就可以从 firebase 获取您的数据作为列表。
Firebase ref = new Firebase("<my-firebase-app>/chapter"):
List<Chapter> chapterList = new ArrayList<Chapter>();
// Now populate your chapterList with the chapters
addItemsInYourChapterList();
ref.setValue(chapterList);
现在像下面这样从 firebase 数据库中检索数据。
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
System.out.println("There are " + snapshot.getChildrenCount() + " chapters");
for (DataSnapshot chapterSnapshot: snapshot.getChildren()) {
Chapter chapter = chapterSnapshot.getValue(Chapter.class);
// Get your chapter details here.
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
请注意,我只考虑了 chapter
节点。根据您的需要修改代码。
关于java - 关于 Android Firebase 检索数据(无 setter /字段错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49933683/