Java – about Android firebase retrieving data (no setter / field error)

I just want to retrieve data from my firebase database, but I don't know the correct way to get the data. This is my error in 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

This is my data class. This is the chapter. Class model

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;
    }
}

And study.class model

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;
   }
}

This is my addvalueeventlistener Code:

 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) {

    }
});

Finally, this is my JSON tree in the firebase database

I tried some methods, but I couldn't solve the mistake

Code execution does not return chapter class data. For example:

subject.getChapter().getChaterName() 
subject.getChapter().getChaterExplain()

Thank you for helping me deal with this problem. Thank you for your concern

resolvent:

There are several errors in your POJO class chapter. First, the error message clearly indicates that the chapterone and chaptertwo properties are missing. You need to be in your POJO

So your chapter. Java should be like this

public class Chapter {
    public ChapterOne chapterOne;
    public ChapterTwo chapterTwo;
}

Chapterone and chaptertwo classes also need to be defined

public class ChapterOne extends ChapterDetails {

}

public class ChapterTwo extends ChapterDetails {

}

public class ChapterDetails {
    public String chapterName;
    public String chapterExplain;
}

Please note that you have misspelled the chapter name and the chapter in the chapter POJO. The current variable names are chatername and chaterexplain, which is wrong. Please fix the variable name

to update

It's impossible to use your current implementation. However, I suggest that you make some changes to your firebase database implementation. Let's modify it with chapter nodes

chapter
    - chapterDetails
        - chapterId : 1
        - chapterName : Something one
        - chapterExplain : Some explanation
    - chapterDetails
        - chapterId : 2
        - chapterName : Something two
        - chapterExplain : Some explanation

If you modify the firebase database structure as above, you may encounter the following POJO classes

public class Chapter {
    List<ChapterDetails> chapterDetails;
}

public class ChapterDetails {
    public Long chapterId;
    public String chapterName;
    public String chapterExplain;
}

I just showed a pseudo implementation. Sorry, if the updated answer is misleading. However, please use the following content to set the data in the firebase database so that you can get the data as a list in 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); 

Now retrieve the data from the firebase database as follows

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());
      }
  });

Please note that I only consider chapter nodes. Modify the code as needed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>