
我只是想从我的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树.
我尝试了一些方法,但我无法解决这个错误.
代码执行不返回Chapter类数据.例如:
subject.getChapter().getChatername() subject.getChapter().getChaterExplain()感谢您帮助我处理这个问题.谢谢你的关怀.
解决方法:
您的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;}请注意,您已拼错了章节名称和章节pojo中的章节.当前变量名称是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 - 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()); } });请注意,我只考虑章节点.根据需要修改代码.
总结以上是内存溢出为你收集整理的java – 关于Android Firebase检索数据(没有setter /字段错误)全部内容,希望文章能够帮你解决java – 关于Android Firebase检索数据(没有setter /字段错误)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)