我試圖將firebase數據庫數據讀取到列表中,我得到「com.google.firebase.database.DatabaseException:未能將類型java.lang.Long的值轉換爲字符串」或nullpointerexception at Products product = ds.getValue(Products.class);
Android Firebase
public class Products {
private String id;
private String reccomended;
public Products(){}
public Products(String id, String reccomended){
this.id = id;
this.reccomended = reccomended;
}
public String getId() {return id;}
public void setId(String id) {this.id = id;}
public String getReccomended() {return reccomended;}
public void setReccomended(String reccomended) {this.reccomended = reccomended;}
}
偵聽器(locationSelected是位置(當前爲 「1」 或 「2」)從噴絲加載的另一個活動通過)
dbRef.child(locationSelected).child("products").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
Products product = ds.getValue(Products.class);
list.add(product);
}
for (int i=0; i<list.size(); i++){
Log.d("LIST: ", list.get(i).toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
例如:
Log.d("LOG: ", ds.getValue().toString());
給出了這樣的輸出:
D/LOG:: {reccomended=7, id=502100}
D/LOG:: {reccomended=10, id=502267}
D/LOG:: {reccomended=15, id=502291}
試'Long.toString(list.get(ⅰ))' – ColonD
問題是在該行 「產品產物= ds.getValue(Products.class);」 – fkvestak