ArrayList and SQLite

+1 Kate Jabbour · March 4, 2015
Hi, would you please give some hints about using ArrayList with  SQLite? 
was it covered in any tutorial? searched but couldn't find among the titles.

Appreciated :)

Post a Reply

Replies

Oldest  Newest  Rating
+1 Mohamed Elkady · March 4, 2015
public List getAllBooks() {
        List books = new ArrayList();
        String query = "select * from " + TABLE_BOOKS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query,null);

        Book book = null;
        if (cursor.moveToFirst()) {
            do {
                book = new Book();
                book.setId(Integer.parseInt(cursor.getString(0)));
                book.setTitle(cursor.getString(1));
                book.setAuthor_name(cursor.getString(2));
                books.add(book);
            } while (cursor.moveToNext());
        }

        Log.d("getAllBooks",books.toString());
        return books;
    }

 List books = mySQLiteHelper.getAllBooks();



hope this example can help you
0 Ícaro Pinho · March 4, 2015
You use the method query from the SQLiteDatabase class, this method returns a cursor with the fields of your database, you can iterate the cursor and populate the ArrayList from it. The example above will help you understand what I just said.
0 Kate Jabbour · March 5, 2015
thank you so much for the help!

would it be possible to send as parameters two separate arraylists or one 2 dimensional arraylist, that would be of integer type? Iam asking regarding the overhead issue.


thanks again!
0 Avanish Kumar · September 12, 2015
check on http://j2sexample.blogspot.com/
  • 1

Java / Android Development

107,020 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator