public List<Product> getProductsCategory(String categoryId) { List<Product> list = new ArrayList<Product>(); SQLiteDatabase db = null; Cursor cursor = null; try { db = mDbHelper.getReadableDatabase(); cursor = db.query( DatabaseHelper.TABLE_PRODUCTS, mAllColumns, DatabaseHelper.TABLE_PRODUCT_CATEGORY_ID + "=?", new String[] {categoryId}, null, null, null, null); } catch (NullPointerException e) { if (AlwarshaApp.DEBUG) Log.e(TAG, "Exception at getProductGroup"); } if (cursor.moveToFirst()) { for (int i = 0; i < cursor.getCount(); i++) { Product product = null; int db_id = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_ID)); String db_name_ar = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_NAME_AR)); String db_name_en = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_NAME)); int db_category_id = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_CAT_ID)); String db_pic_name = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_PIC_NAME)); float db_price = cursor.getFloat(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_PRICE)); product = new Product(db_id, db_name_en, db_category_id, db_pic_name, db_price, "EN"); product.addName("AR", db_name_ar); list.add(product); cursor.moveToNext(); } } cursor.close(); return list; }
public Product getProduct(int id) { Product product = null; SQLiteDatabase db = null; Cursor cursor = null; try { db = mDbHelper.getReadableDatabase(); cursor = db.query( DatabaseHelper.TABLE_PRODUCTS, mAllColumns, DatabaseHelper.TABLE_PRODUCT_ID + "=?", new String[] {String.valueOf(id)}, null, null, null, null); } catch (NullPointerException e) { if (AlwarshaApp.DEBUG) Log.e(TAG, "Exception at getProduct"); } if (cursor.moveToFirst()) { int db_id = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_ID)); String db_name_ar = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_NAME_AR)); String db_name_en = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_NAME)); int db_category_id = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_CAT_ID)); String db_pic_name = cursor.getString(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_PIC_NAME)); float db_price = cursor.getFloat(cursor.getColumnIndex(DatabaseHelper.TABLE_PRODUCT_PRICE)); cursor.close(); product = new Product(db_id, db_name_en, db_category_id, db_pic_name, db_price, "EN"); product.addName("AR", db_name_ar); } return product; }