Reputation: 71
I'm creating a simple library app where books can be added and a list of books can be viewed.
here is my book class
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;
} }
and here is where I am trying to read from the values which are already written into 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);
}}
the error I am getting is
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)
Someone please explain why this is happening and how I could get the list view to list these books that have been put into firebase.
Upvotes: 4
Views: 11627
Reputation: 1
Your doing it wrong this is how you get it databasereference reference = FirebaseDatabase.getInstance().getReference("books") if you have a child on that then just add .child( "like fuser.getUid() something like this" )
once that's done then add a Listener for it, now go to onDataChange add your code like this String userName = snapshot.child("Data Name").getValue().toString();
Upvotes: 0
Reputation: 90
Your code:
DatabaseReference ref = database.getReference();
DatabaseReference temp = ref.child("books");
Updated code with correction to solve error:
DatabaseReference ref = database.getReference();
DatabaseReference temp = ref.child("books").toString();
Upvotes: 0
Reputation: 191701
dataSnapshot.getChildren()
is going to return you a list of attributes for a book, not a Book
object itself. One of those is a String, thus the error
You need another level of data in the database, or if all you're storing is a list of books, remove child("books")
and use a generated unique key instead of the string "books"
Additionally, Firebase is going to try to set your fields using camelCase
methods and variables (like those in the database), not underscores like those in your Java class. In other words, they should match
Upvotes: 1