Firebase无法将java.lang.String类型的对象转换为类型

时间:2017-04-18 16:57:02

标签: android firebase firebase-realtime-database

我正在创建一个简单的图书馆应用程序,可以添加书籍并查看书籍列表。

这是我的书类

public class Book {

private int _id;
private String _bookTitle;
private String _bookAuthor;
private int _bookISBN;

private Map<String, Object> mp;

public Book() {

}

public Book(String _bookTitle, String _bookAuthor, int _bookISBN) {
    this._id = _id;
    this._bookTitle = _bookTitle;
    this._bookAuthor = _bookAuthor;
    this._bookISBN = _bookISBN;

}

public int get_id() {
    return _id;
}

public void set_id(int _id) {
    this._id = _id;
}

public String get_bookTitle() {
    return _bookTitle;
}

public void set_bookTitle(String _bookTitle) {
    this._bookTitle = _bookTitle;
}

public String get_bookAuthor() {
    return _bookAuthor;
}

public void set_bookAuthor(String _bookAuthor) {
    this._bookAuthor = _bookAuthor;
}

public int get_bookISBN() {
    return _bookISBN;
}

public void set_bookISBN(int _bookISBN) {
    this._bookISBN = _bookISBN;
}


public Map<String, Object> toMap() {
    mp = new HashMap<>();
    mp.put("bookTitle", this._bookTitle);
    mp.put("bookAuthor", this._bookAuthor);
    mp.put("bookISBN", this._bookISBN);


    return mp;
} }

这是我试图从已经写入firebase的值中读取的地方

snapshot of firebase

public class ViewBook extends AppCompatActivity {


ArrayList<String> myArrayList = new ArrayList<>();

final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference();
//  DatabaseReference mPostReference = ref.child("books");
DatabaseReference temp = ref.child("books");

ArrayAdapter<String> adapter;

@Override
public void onStart(){
    super.onStart();


    ValueEventListener postListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Book book = postSnapshot.getValue(Book.class);

                myArrayList.add(book.get_bookTitle() + "\n" + book.get_bookAuthor()  + "\n" + book.get_bookISBN());


            }

            ListView lv = (ListView)findViewById(R.id.list);
            adapter = new ArrayAdapter<String>(ViewBook.this, android.R.layout.simple_list_item_1, myArrayList);
            lv.setAdapter(adapter);
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {
            //Getting Post failed, log a message
            Log.w("VIEWBOOKS", "loadPost:onCancelled", databaseError.toException());

        }
    };
    temp.addValueEventListener(postListener);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_book);
}}

我得到的错误是

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.taylorcampos.androidfirebasedatabase, PID: 25339
              com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.taylorcampos.androidfirebasedatabase.Book
                  at com.google.android.gms.internal.zzbtg.zze(Unknown Source)
                  at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                  at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                  at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                  at com.example.taylorcampos.androidfirebasedatabase.ViewBook$1.onDataChange(ViewBook.java:38)
                  at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
                  at com.google.android.gms.internal.zzbqx.zzZT(Unknown Source)
                  at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

有人请解释为什么会发生这种情况以及如何获取列表视图以列出已放入firebase的这些书籍。

2 个答案:

答案 0 :(得分:1)

dataSnapshot.getChildren()将返回一本书的属性列表,而不是Book对象本身。其中一个是String,因此错误

您需要数据库中的其他级别的数据,或者如果您要存储的所有内容都是书籍列表,请删除child("books")并使用生成的唯一键而不是字符串"books"

此外,Firebase将尝试使用camelCase方法和变量(如数据库中的那些)设置字段,而不是像Java类中那样的下划线。换句话说,它们应匹配

答案 1 :(得分:0)

您的代码:

DatabaseReference ref = database.getReference();
DatabaseReference temp = ref.child("books");

更新了带纠正的代码以解决错误:

DatabaseReference ref = database.getReference();
DatabaseReference temp = ref.child("books").toString();