Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java/Android-从Firebase加载ArrayList作为对象时出错_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java/Android-从Firebase加载ArrayList作为对象时出错

Java/Android-从Firebase加载ArrayList作为对象时出错,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我们已经尝试解决Firebase加载arraylist的问题一段时间了,但不知道如何解决它 对象类 public class Book { private String title; private String author; private String ISBN; private String photo; private String ownerEmail; private String ownerID; private Strin

我们已经尝试解决Firebase加载arraylist的问题一段时间了,但不知道如何解决它

对象类

public class Book {
    private String title;
    private String author;
    private String ISBN;
    private String photo;
    private String ownerEmail;
    private String ownerID;
    private String BookID;
    private Date returnDate;
    private Status status;
    private String borrowerID;

    public Book(){}
    public Book(String title, String author, String ISBN, String photo, String ownerEmail, String ownerID) {
        this.title = title;
        this.author = author;
        this.ISBN = ISBN;
        this.photo = photo;
        this.ownerEmail = ownerEmail;
        this.ownerID = ownerID;
        this.status = Status.Available;
        if(this.BookID == null || this.BookID.isEmpty())
            this.BookID = UUID.randomUUID().toString();
    }
    public enum Status{
        Available,
        Requested,
        Accepted,
        Borrowed
    }
所有变量都有getter和setter,我要做的是使用代码从firebase加载这些信息:

 public void loadMyBookFromFirebase(){
        final ArrayList<Book> bookList = new ArrayList<>();

        DatabaseReference userReference = FirebaseDatabase.getInstance().getReference().child("users").child(this.userID).child("myBooks");
        userReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.d("testing","books" + dataSnapshot.toString());
                if(dataSnapshot.exists()) {
                    try {
                        for (DataSnapshot books : dataSnapshot.getChildren()) {
                            Book book = dataSnapshot.getValue(Book.class);
                            Log.d("testing","books" + book.getAuthor());
                            //load book into ListView
                        }
                    } catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.w("testing","Error: ", databaseError.toException());
            }
        });
    }
public void loadMyBookFromFirebase(){
最终ArrayList bookList=新ArrayList();
DatabaseReference userReference=FirebaseDatabase.getInstance().getReference().child(“用户”).child(this.userID).child(“myBooks”);
userReference.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
Log.d(“测试”、“书籍”+dataSnapshot.toString());
if(dataSnapshot.exists()){
试一试{
对于(DataSnapshot书籍:DataSnapshot.getChildren()){
Book Book=dataSnapshot.getValue(Book.class);
Log.d(“测试”、“书籍”+book.getAuthor());
//将书本加载到ListView中
}
}捕获(例外e){
e、 printStackTrace();
}
}
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
Log.w(“测试”,“错误:”,databaseError.toException());
}
});
}
这是我的firebase数据库 我不断地得到一份工作 无法将类型为
java.util.ArrayList
的对象转换为类型
com.example.cmput301w19t15.Book

Book Book=dataSnapshot.getValue(Book.class)上出错
我不知道为什么会出现convert对象错误,我不想搜索单个变量并设置它们,这会破坏对象类的用途


我真的很想了解一下这个问题。要解决这个问题,请更改以下代码行:

Book book = dataSnapshot.getValue(Book.class);


您需要从
books
对象获取数据,而不是从
dataSnapshot
对象获取数据。

请上传数据库结构数据库很多,这是一个疏忽,根本没有注意到
Book book = books.getValue(Book.class);