Search in sources :

Example 6 with ShoppingListItem

use of com.se491.chef_ly.model.ShoppingListItem in project chefly_android by chef-ly.

the class DatabaseHandler method getShoppingList.

public ArrayList<ShoppingListItem> getShoppingList() {
    SQLiteDatabase db = null;
    Cursor cursor = null;
    ArrayList<ShoppingListItem> listItems = new ArrayList<>();
    try {
        db = this.getReadableDatabase();
        cursor = db.query(ShoppingList.TABLE_LIST_ITEMS, ShoppingList.ALL_COLUMNS, null, null, null, null, ShoppingList.COLUMN_NAME);
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(cursor.getColumnIndex(ShoppingList.COLUMN_LIST_ID));
                String name = cursor.getString(cursor.getColumnIndex(ShoppingList.COLUMN_NAME));
                boolean purchased = cursor.getInt(cursor.getColumnIndex(ShoppingList.COLUMN_PURCHASED)) > 0;
                listItems.add(new ShoppingListItem(id, name, purchased));
            } while (cursor.moveToNext());
            return listItems;
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
        if (db != null) {
            db.close();
        }
    }
    return listItems;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) ShoppingListItem(com.se491.chef_ly.model.ShoppingListItem) Cursor(android.database.Cursor)

Aggregations

ShoppingListItem (com.se491.chef_ly.model.ShoppingListItem)6 TextView (android.widget.TextView)3 ArrayList (java.util.ArrayList)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 View (android.view.View)2 DatabaseHandler (com.se491.chef_ly.Databases.DatabaseHandler)2 Context (android.content.Context)1 Cursor (android.database.Cursor)1 Bitmap (android.graphics.Bitmap)1 KeyEvent (android.view.KeyEvent)1 RotateAnimation (android.view.animation.RotateAnimation)1 InputMethodManager (android.view.inputmethod.InputMethodManager)1 BaseAdapter (android.widget.BaseAdapter)1 Button (android.widget.Button)1 EditText (android.widget.EditText)1 ImageButton (android.widget.ImageButton)1 ImageView (android.widget.ImageView)1 ListView (android.widget.ListView)1 RelativeLayout (android.widget.RelativeLayout)1 ScrollView (android.widget.ScrollView)1