In the GetRecipes method, i am facing problem. I have trouble retrieving the integers (Which as Recipe_ID).......and putting that in an Integer Array List. I have written code to do so, but it wont be displayed in logcat. Please help. Thanks Sonali
My Code:
package com.freedomkitchen.sonali.freedomkitchenAndroidApp;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DB_Access extends SQLiteOpenHelper {
static int x = 0;
ArrayList<String> User_Main_Ingredients;
ArrayList<String> Recipe_Main_Ingredients;
public static List<Integer> recipes = new ArrayList<Integer>();
int[] matchedRecipes = matchedRecipes = new int[200];
public static final String DATABASE_NAME = "FreedomKitchenDatabase.db";
//********** LOGIN TABLE COLUMNS *************
public static final String LOGIN_TABLE_NAME = "login_details";
public static final String LOGIN_COLUMN_ID = "user_id";
public static final String LOGIN_COLUMN_USERNAME = "username";
public static final String LOGIN_COLUMN_PASSWORD = "password";
//********** REGISTRATION TABLE COLUMNS *************
public static final String REG_TABLE_NAME = "user_details";
public static final String REG_COLUMN_ID = "user_id";
public static final String REG_COLUMN_FIRST_NAME = "first_name";
public static final String REG_COLUMN_LAST_NAME = "last_name";
public static final String REG_COLUMN_GENDER = "gender";
public static final String REG_COLUMN_AGE = "age";
public static final String REG_COLUMN_COUNTRY = "country";
//********** FOOD_CATEGORY TABLE COLUMNS *************
public static final String FOOD_CAT_TABLE_NAME = "food_category";
public static final String FOOD_COLUMN_ID = "food_category_id";
public static final String FOOD_CATEGORY_NAME = "food_category_name";
//********** INGREDIENTS TABLE COLUMNS *************
public static final String INGREDIENTS_TABLE_NAME = "ingredients";
public static final String INGREDIENT_FOOD_CAT = "food_category_id";
public static final String INGREDIENT_NAME = "ingredient_name";
//********** RECIPES TABLE COLUMNS *************
public static final String RECIPES_TABLE_NAME = "recipes";
public static final String RECIPE_COLUMN_ID = "recipe_id";
public static final String RECIPE_CATEGORY = "recipe_category";
public static final String MEAL_CATEGORY = "meal_category";
public static final String RECIPE_NAME = "recipe_name";
public static final String RECIPE_INSTRUCTIONS = "recipe_instructions";
//********** RECIPE_INGREDIENTS TABLE COLUMNS *************
public static final String RECIPE_ING_TABLE_NAME = "recipe_ingredients";
public static final String RECIPE_ID = "recipe_id";
public static final String RECIPE_MAIN_ING = "Main_ing_name";
public DB_Access(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + REG_TABLE_NAME + "(" + REG_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + REG_COLUMN_FIRST_NAME + " TEXT," + REG_COLUMN_LAST_NAME + " TEXT," + REG_COLUMN_GENDER + " TEXT," + REG_COLUMN_AGE + " TEXT," + REG_COLUMN_COUNTRY + " TEXT)");
db.execSQL("create table " + LOGIN_TABLE_NAME + "(" + LOGIN_COLUMN_ID + " INTEGER," + LOGIN_COLUMN_USERNAME + " TEXT," + LOGIN_COLUMN_PASSWORD + " TEXT,FOREIGN KEY(" + LOGIN_COLUMN_ID + ")REFERENCES " + REG_TABLE_NAME + "(" + REG_COLUMN_ID + "))");
db.execSQL("create table " + FOOD_CAT_TABLE_NAME + "(" + FOOD_COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + FOOD_CATEGORY_NAME + " TEXT" + ")");
db.execSQL("create table " + INGREDIENTS_TABLE_NAME + "(" + INGREDIENT_NAME + " TEXT PRIMARY KEY," + INGREDIENT_FOOD_CAT + " INTEGER,FOREIGN KEY(" + INGREDIENT_FOOD_CAT + ")REFERENCES " + FOOD_CAT_TABLE_NAME + "(" + FOOD_COLUMN_ID + "))");
db.execSQL("create table " + RECIPES_TABLE_NAME + "(" + RECIPE_COLUMN_ID + " INTEGER PRIMARY KEY," + RECIPE_CATEGORY + " TEXT," + MEAL_CATEGORY + " TEXT," + RECIPE_NAME + " TEXT," + RECIPE_INSTRUCTIONS + " TEXT)");
db.execSQL("create table " + RECIPE_ING_TABLE_NAME + "(" + RECIPE_ID + " INTEGER," + RECIPE_MAIN_ING + " TEXT," + " INTEGER,FOREIGN KEY(" + RECIPE_ID + ")REFERENCES " + RECIPES_TABLE_NAME + "(" + RECIPE_COLUMN_ID + ")," + "FOREIGN KEY(" + RECIPE_MAIN_ING + ")REFERENCES " + INGREDIENTS_TABLE_NAME + "(" + INGREDIENT_NAME + "))");
}
public void addFoodCat(String foodcat) {
SQLiteDatabase mydb = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(FOOD_CATEGORY_NAME, foodcat);
mydb.insert(FOOD_CAT_TABLE_NAME, null, contentValues);
}
public void addIngredientsVeg(String Veg) {
SQLiteDatabase mydb = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(INGREDIENT_FOOD_CAT, 1);
contentValues.put(INGREDIENT_NAME, Veg);
mydb.insert(INGREDIENTS_TABLE_NAME, null, contentValues);
}
public void addIngredientsFruits(String Fruit) {
SQLiteDatabase mydb = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(INGREDIENT_FOOD_CAT, 2);
contentValues.put(INGREDIENT_NAME, Fruit);
mydb.insert(INGREDIENTS_TABLE_NAME, null, contentValues);
}
public void addIngredientsDairy(String Dairy) {
SQLiteDatabase mydb = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(INGREDIENT_FOOD_CAT, 3);
contentValues.put(INGREDIENT_NAME, Dairy);
mydb.insert(INGREDIENTS_TABLE_NAME, null, contentValues);
}
public void addIngredientsGrains(String Grains) {
SQLiteDatabase mydb = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(INGREDIENT_FOOD_CAT, 4);
contentValues.put(INGREDIENT_NAME, Grains);
mydb.insert(INGREDIENTS_TABLE_NAME, null, contentValues);
}
public void addIngredientsSeafood(String seafood) {
SQLiteDatabase mydb = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(INGREDIENT_FOOD_CAT, 5);
contentValues.put(INGREDIENT_NAME, seafood);
mydb.insert(INGREDIENTS_TABLE_NAME, null, contentValues);
}
public boolean adduserRecipes(String Food_Cat, String meal_cat, String Recipe_Name, ArrayList<String> Main_Ingredients, String Instructions) {
SQLiteDatabase db = this.getWritableDatabase();
SQLiteDatabase mydb = this.getReadableDatabase();
Cursor cursor2= mydb.rawQuery("select * from " + RECIPES_TABLE_NAME, null);
int n=cursor2.getCount();
int r_id=n+1;
ArrayList<String> main_ing=new ArrayList<String>();
main_ing=Main_Ingredients;
String ins=Instructions;
//recipe table
ContentValues contentValues_one = new ContentValues();
contentValues_one.put(RECIPE_COLUMN_ID, r_id);
contentValues_one.put(RECIPE_CATEGORY, Food_Cat);
contentValues_one.put(MEAL_CATEGORY, meal_cat);
contentValues_one.put(RECIPE_NAME, Recipe_Name);
contentValues_one.put(RECIPE_INSTRUCTIONS, Instructions);
//recipe_ingredients
ContentValues contentValues_two = new ContentValues();
for(int i=0;i<main_ing.size();i++){
String item=main_ing.get(i);
contentValues_two.put(RECIPE_ID,r_id);
contentValues_two.put(RECIPE_MAIN_ING,item);
db.insert(RECIPE_ING_TABLE_NAME, null, contentValues_two);
}
db.insert(RECIPES_TABLE_NAME, null, contentValues_one);
return true;
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + REG_TABLE_NAME);
onCreate(db);
}
public boolean checkLogin(String username, String password) throws SQLException {
SQLiteDatabase db = this.getReadableDatabase();
Cursor mCursor = db.rawQuery("SELECT * FROM " + LOGIN_TABLE_NAME + " WHERE " + LOGIN_COLUMN_USERNAME + "=? AND " + LOGIN_COLUMN_PASSWORD + "=?", new String[]{username, password});
if (mCursor != null) {
if (mCursor.getCount() > 0) {
return true;
}
}
return false;
}
public boolean Registration(String first_name, String last_name, String age, String gender, String country, String username, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(REG_COLUMN_FIRST_NAME, first_name);
contentValues.put(REG_COLUMN_LAST_NAME, last_name);
contentValues.put(REG_COLUMN_GENDER, gender);
contentValues.put(REG_COLUMN_AGE, age);
contentValues.put(REG_COLUMN_COUNTRY, country);
ContentValues mContentValues1 = new ContentValues();
mContentValues1.put(LOGIN_COLUMN_USERNAME, username);
mContentValues1.put(LOGIN_COLUMN_PASSWORD, password);
db.insert(LOGIN_TABLE_NAME, null, mContentValues1);
db.insert(REG_TABLE_NAME, null, contentValues);
return true;
}
public ArrayList<String> getIngredients(int id) {
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<String> array_list = new ArrayList<String>();
Cursor cursor = db.rawQuery("SELECT " + INGREDIENT_NAME + " FROM " + INGREDIENTS_TABLE_NAME + " where " + INGREDIENT_FOOD_CAT + "=" + id, null);
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
array_list.add(cursor.getString(cursor.getColumnIndex(INGREDIENT_NAME)));
cursor.moveToNext();
}
return array_list;
}
public void GetRecipes(String meal_cat_selected, ArrayList<String> User_Main_Ing) {
SQLiteDatabase db = this.getReadableDatabase();
User_Main_Ingredients = User_Main_Ing;
for (int i = 0; i <User_Main_Ingredients.size(); i++) {
Log.i("User_Main_Ingredients", User_Main_Ingredients.get(i).toString());
}
String mealcat=meal_cat_selected;
Cursor cursor_recipes = db.rawQuery("SELECT " + RECIPE_COLUMN_ID + " FROM " + RECIPES_TABLE_NAME + " where " + MEAL_CATEGORY + "=" + mealcat, null);
int i,id;
cursor_recipes.moveToFirst();
while (cursor_recipes.isAfterLast() == false) {
recipes.add(cursor_recipes.getInt(cursor_recipes.getColumnIndex(RECIPE_COLUMN_ID)));
cursor_recipes.moveToNext();
}
for (i = 0; i < recipes.size(); i++) {
Log.i("Recipes",Integer.toString(recipes.get(i)));
}
for (i = 0; i < recipes.size(); i++) {
id = recipes.get(i);
boolean result = checkrecipe(id);
if (result == true) {
matchedRecipes[i] = i;
}
}
}
public boolean checkrecipe(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursorRecipeMain = db.rawQuery("SELECT " + RECIPE_MAIN_ING+ " FROM " + RECIPE_ING_TABLE_NAME + " where " + RECIPE_ID + "=" + id, null);
cursorRecipeMain.moveToFirst();
int i=0;
while (cursorRecipeMain.isAfterLast() == false) {
Recipe_Main_Ingredients.add(cursorRecipeMain.getString(cursorRecipeMain.getColumnIndex(INGREDIENT_NAME)));
cursorRecipeMain.moveToNext();
}
//disp recipe_main_ing
for (i = 0; i <Recipe_Main_Ingredients.size(); i++) {
Log.i("recipe_main_ing", Recipe_Main_Ingredients.get(i));
}
for(i=0;i<=User_Main_Ingredients.size();) {
if(Recipe_Main_Ingredients.get(i)==(User_Main_Ingredients.get(i)))
{
x++;
i=0;
if(x==Recipe_Main_Ingredients.size()){
return true;
}
}
else
{
if(x==User_Main_Ingredients.size()){
break;
}
i++;
}
}
return false;
}
}
Aucun commentaire:
Enregistrer un commentaire