I have created an external sqlite database with book information stored in it. Its store in the asset folder. I have created a book object, but now i am not sure how to store values from database in it. I have used a list function to retrieve the values from database using the sqliteaccesshelper library and called it in the main activity. It would really help if someone suggests me how to store the values in my book object. This is the tutorial that i have followed.
My book object is book() with four parameters.
public book(String title, String author, byte[] image, String issue) {
Title = title;
Author = author;
Image = image;
Issue = issue;
}
This is the Database Function. Note that its not proper and i am not sure how to make it store the value in my Book object.
public List<String> getdetails() {
List<String> lstBook = new ArrayList<>();
Cursor cursor = database.rawQuery("SELECT Book_name, Author, Book_cover, Issue_status FROM books", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
//This line is showing an error.
lstBook.add(cursor.getString(cursor.getColumnIndex("Book_name")), cursor.getString(cursor.getColumnIndex("Author")), cursor.getBlob(cursor.getColumnIndex("Book_cover")), cursor.getString(cursor.getColumnIndex("Issue_Status")));
book Book = new book(); //this is the book object
Book.setTitle(cursor.getString(0));
Book.setAuthor(cursor.getString(1));
Book.setImage(cursor.getBlob(2));
Book.setIssue(cursor.getString(3));
cursor.moveToNext();
}
cursor.close();
return lstBook;
}
This is my MainActivity
// Open the database
databaseAccess.open();
List<String> lstBook = databaseAccess.getdetails();
RecyclerView myrv = (RecyclerView) findViewById(R.id.recyclerview_id);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this, lstBook);
myrv.setLayoutManager(new GridLayoutManager(this, 2));
myrv.setAdapter(myAdapter;
// Close the database
databaseAccess.close();
}
And this is my RecyclerView Adapter.(I am not sure about the set image part though)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.bookname.setText(mData.get(position).getTitle());
holder.bookauthor.setText(mData.get(position).getAuthor());
byte[] data = mData.get(position).getImage(); Bitmap image = toBitmap(data);
holder.bookimg.setImageBitmap(image);
holder.issuestatus.setText(mData.get(position).getIssue());
}