joielechong

Sample of Get All Table Data To ArrayList

Mar 16th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. // Category is an POJO with id and name properties.
  2. // DBHelper.TABLE_CATEGORY is name of category table.
  3. // DBHelper.KEY_ID is column id of category table.
  4. // DBHelper.KEY_NAME is column name of category table.
  5.  
  6. // Database fields
  7. private String[] allColumns = { DBHelper.KEY_ID, DBHelper.KEY_NAME };
  8.  
  9. public ArrayList<Category> getAllCategory() {
  10.     ArrayList<Category> categories = new ArrayList<String>();
  11.     Cursor cursor = database.query(DBHelper.TABLE_CATEGORY, allColumns,
  12.                 null, null, null, null, null);
  13.     if (cursor != null && cursor.moveToFirst()) {
  14.         while (!cursor.isAfterLast()) {
  15.             Category category = new Category();
  16.             category.setId(c.getInt(c.getColumnIndex(DBHelper.KEY_ID)));
  17.             category.setName(c.getString(c.getColumnIndex(DBHelper.KEY_NAME)));
  18.             categories.add(category);
  19.             cursor.moveToNext();
  20.         }
  21.         cursor.close();
  22.     }
  23.     return categories;
  24. }
Add Comment
Please, Sign In to add comment