Java 从云Firestore Android检索复杂对象
我试图从Java 从云Firestore Android检索复杂对象,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我试图从云Firestore数据库中检索一组复杂对象。这是我的班级: public class Book { private String id; private String name; private String original; private ArrayList<Author> authors; private ArrayList<Translator> translators; private Series s
云Firestore
数据库中检索一组复杂对象。这是我的班级:
public class Book {
private String id;
private String name;
private String original;
private ArrayList<Author> authors;
private ArrayList<Translator> translators;
private Series series;
private Float no;
private String seriesNo;
private Integer published;
private Borrower borrower;
private Boolean isCollection;
private Status status;
}
从数据库检索保存的值的最佳方法是什么?很难找到一种同时访问所有值的好方法。我目前使用以下方法检索作者和翻译人员(ArrayList)的值除外
public void getBooks(final BookRetrievable BookRetrievable,String userId){
FirebaseFirestore=FirebaseFirestore.getInstance();
ArrayList books=新建ArrayList();
firebaseFirestore.收藏(“书籍”)
.get()
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
对于(DocumentSnapshot DocumentSnapshot:task.getResult()){
书=新书();
book.setId(documentSnapshot.getString(“id”);
book.setName(documentSnapshot.getString(“名称”));
book.setOriginal(documentSnapshot.getString(“original”);
系列=新系列();
setId(documentSnapshot.getString(“series.id”);
series.setName(documentSnapshot.getString(“series.name”);
丛书(系列);
book.setNo(documentSnapshot.getDouble(“no”).floatValue();
book.setSeriesNo(documentSnapshot.getString(“seriesNo”);
book.setPublished(documentSnapshot.getLong(“published”).intValue());
借款人=新借款人();
setId(documentSnapshot.getString(“series.id”);
follower.setNameInEnglish(documentSnapshot.getString(“series.nameInEnglish”);
借款人.setNameInSinhalese(documentSnapshot.getString(“series.nameinsinsinhalese”);
setFacebook(documentSnapshot.getString(“series.facebook”);
setMobile(documentSnapshot.getString(“series.mobile”);
帐面收款人(借款人);
book.setIsCollection(documentSnapshot.getBoolean(“isCollection”);
book.setStatus(Status.valueOf(documentSnapshot.getString(“Status”));
图书。添加(图书);
}
books.sort((bookb1,bookb2)->b1.getName().compareTo(b2.getName());
可检索书籍。一次性回拨(书籍);
}否则{
}
}
});
}
有什么比这更好的方法吗?请告诉我一次检索ArrayList的最佳方法是什么
为了便于理解,我添加了一个已保存在数据库中的示例的屏幕截图:
提前谢谢
将此Book对象保存到数据库的最佳方式是什么
将数据添加到数据中的方式没有错误。没有最好的办法。最好的解决方案是适合您的需要并使您的工作更轻松的解决方案。如果您在使用地图时感到舒适,请让代码保持原样。如果您需要另一种方法,我可以为您提供另一种解决方案,即使用模型(POJO)类。这就是你的书籍
课程的样子:
public class Book {
private String id;
private String name;
private String original;
private ArrayList<Author> authors;
private ArrayList<Translator> translators;
private Series series;
private Float no;
private String seriesNo;
private Integer published;
private Borrower borrower;
private Boolean isCollection;
private Status status;
public Book() {}
public Book(String id, String name, String original, ArrayList<Author> authors, ArrayList<Translator> translators, Series series, Float no, String seriesNo, Integer published, Borrower borrower, Boolean isCollection, Status status) {
this.id = id;
this.name = name;
this.original = original;
this.authors = authors;
this.translators = translators;
this.series = series;
this.no = no;
this.seriesNo = seriesNo;
this.published = published;
this.borrower = borrower;
this.isCollection = isCollection;
this.status = status;
}
public String getId() { return id; }
public String getName() { return name; }
public String getOriginal() { return original; }
public ArrayList<Author> getAuthors() { return authors; }
public ArrayList<Translator> getTranslators() { return translators; }
public Series getSeries() { return series; }
public Float getNo() { return no; }
public String getSeriesNo() { return seriesNo; }
public Integer getPublished() { return published; }
public Borrower getBorrower() { return borrower; }
public Boolean getCollection() { return isCollection; }
public Status getStatus() { return status; }
}
公共课堂教材{
私有字符串id;
私有字符串名称;
私人字符串原件;
private ArrayList更多详细信息。感谢您的评论。我非常了解异步行为,需要澄清我所做的是否错误,以及是否有更好、更简单的方法。我通过另一次回拨检索了我的子收集数据,现在一切似乎都失败了地点:)
public void getBooks(final BookRetrievable bookRetrievable, String userId) {
FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
ArrayList<Book> books = new ArrayList<>();
firebaseFirestore.collection("books")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot documentSnapshot : task.getResult()) {
Book book = new Book();
book.setId(documentSnapshot.getString("id"));
book.setName(documentSnapshot.getString("name"));
book.setOriginal(documentSnapshot.getString("original"));
Series series = new Series();
series.setId(documentSnapshot.getString("series.id"));
series.setName(documentSnapshot.getString("series.name"));
book.setSeries(series);
book.setNo(documentSnapshot.getDouble("no").floatValue());
book.setSeriesNo(documentSnapshot.getString("seriesNo"));
book.setPublished(documentSnapshot.getLong("published").intValue());
Borrower borrower = new Borrower();
borrower.setId(documentSnapshot.getString("series.id"));
borrower.setNameInEnglish(documentSnapshot.getString("series.nameInEnglish"));
borrower.setNameInSinhalese(documentSnapshot.getString("series.nameInSinhalese"));
borrower.setFacebook(documentSnapshot.getString("series.facebook"));
borrower.setMobile(documentSnapshot.getString("series.mobile"));
book.setBorrower(borrower);
book.setIsCollection(documentSnapshot.getBoolean("isCollection"));
book.setStatus(Status.valueOf(documentSnapshot.getString("status")));
books.add(book);
}
books.sort((Book b1, Book b2) -> b1.getName().compareTo(b2.getName()));
bookRetrievable.onCallback(books);
} else {
}
}
});
}
public class Book {
private String id;
private String name;
private String original;
private ArrayList<Author> authors;
private ArrayList<Translator> translators;
private Series series;
private Float no;
private String seriesNo;
private Integer published;
private Borrower borrower;
private Boolean isCollection;
private Status status;
public Book() {}
public Book(String id, String name, String original, ArrayList<Author> authors, ArrayList<Translator> translators, Series series, Float no, String seriesNo, Integer published, Borrower borrower, Boolean isCollection, Status status) {
this.id = id;
this.name = name;
this.original = original;
this.authors = authors;
this.translators = translators;
this.series = series;
this.no = no;
this.seriesNo = seriesNo;
this.published = published;
this.borrower = borrower;
this.isCollection = isCollection;
this.status = status;
}
public String getId() { return id; }
public String getName() { return name; }
public String getOriginal() { return original; }
public ArrayList<Author> getAuthors() { return authors; }
public ArrayList<Translator> getTranslators() { return translators; }
public Series getSeries() { return series; }
public Float getNo() { return no; }
public String getSeriesNo() { return seriesNo; }
public Integer getPublished() { return published; }
public Borrower getBorrower() { return borrower; }
public Boolean getCollection() { return isCollection; }
public Status getStatus() { return status; }
}