samedi 27 février 2016

Retrieving data from an sqlite database

So I'm doing a cooking application, I'm new to coding

I have created an sqlite database with a few recipes

DB_Recipe.java

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DB_Recipe extends SQLiteOpenHelper {
final static String DB_NAME = "db_recipe";

public DB_Recipe(Context context) {
    super(context, DB_NAME, null, 1);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE IF NOT EXISTS recipe(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, ingredients TEXT, preparation TEXT, img BLOB)";
    db.execSQL(sql);

    ContentValues values = new ContentValues();

    values.put("Name", "Chicken Curry");
    values.put("Ingredients", "1 Onion; diced\n" +
            "2 cloves Garlic; minced\n" +
            "1 28-oz can tomatoes\n" +
            "2 tbsp curry; heaping tbsps.\n" +
            "1/3 cups mango chutney\n" +
            "1/2 cups Water\n" +
            "2 chicken breasts; cooked (at most 3)\n" +
            "Makes 4 Servings");
    values.put("Preparation","In large saucepan, gently cook onion for approx 10 minutes or until clear. Add garlic, cook 1 minute. Add curry paste and chutney, cook 1 minute. Add tomatoes and water, simmer 15-20 minutes. Add cooked chicken, cook 5-8 minutes. Serve over rice!");
    values.put("img", R.drawable.chickencurry);
    db.insert("Recipe", "_id", values);

    values.put("Name", "Baked Chicken Cordon Bleu");
    values.put("Ingredients", "5 chicken breasts; split in 2 (total yield 10 pieces)\n" +
            "4 tsps onion powder; divided\n" +
            "3 cups fresh bread crumbs\n" +
            "3/4 cup shredded Parmesan\n" +
            "salt/pepper; to taste\n" +
            "4 tbsp butter; melted\n" +
            "3 tbsp olive oil; or canola\n" +
            "10 slices deli ham; sliced thinly\n" +
            "10 slices Gruyere/Swiss cheese\n" +
            "basil/parsley; optional sprinkle\n" +
            "Makes 10 Servings");
    values.put("Preparation","Process a few slices of bread in food processor to make about 4-5 cups of fresh breadcrumbs. Add Parmesan and 2 tsp onion powder. Set aside. Melt butter in a bowl and mix with oil. Set aside. Wash and clean chicken breasts. Pat dry and cut each breast into two pieces. You will have two pretty thin slices. Place a chicken piece on a cutting board. Lay a plastic wrap over the chicken piece and pound with a mallet to about a 1/4 inch thickness. Take care not to pound too hard because the meat may tear or create holes. Do the same thing with all the chicken pieces. You should have 10 chicken pieces to the original 5.\n" +
            "\n" +
            "Season each chicken piece with a bit of salt/pepper and the remaining 2 tsp onion powder. Be careful not to over salt the chicken as it is quite thin and the cheese and ham are also salty.\n" +
            "\n" +
            "Bring all the other ingredients in close proximity. Take out the deli ham and place on a plate. Take cheese out...I usually like to cut the cheese slices in half in case I have to overlap them. Take a chicken piece and place a slice of ham and a slice of cheese (or 2 if you cut the original in half). Roll chicken... making sure you tuck the sides in as much as possible (this prevents the cheese from oozing out too much in the final baking time). You can secure it with a toothpick. I have, as of lately, started to omit this step. But if you are doing this for the first time, it might help to secure it with the toothpicks. Take the chicken roll and dip into the melted butter/oil... make sure all the sides are well coated.\n" +
            "\n" +
            "Take the butter coated chicken roll and then roll it in the breadcrumbs/Parmesan mixture. Place in a casserole dish or plate. Finish with the rest. You can sprinkle some dried herbs of your choice if you like. I sprinkled some dried basil. Place in the refrigerator for at least an hour to ensure that the breadcrumbs stay put on the chicken :). When ready to bake, place each chicken roll on a parchment lined jelly roll pan (this will help with the cleaning later) making sure you don't overcrowd them. Bake in a preheated 375 oven for 15-20 minutes. Place the pan on the top third rack. Don't over cook the chicken. You can easily do this in a toaster oven as well.");
    values.put("img", R.drawable.bakedchickencordonbleu);
    db.insert("Recipe", "_id", values);

    values.put("Name", "Broccoli and Ham Potato Soup");
    values.put("Ingredients", "3 tablespoons Butter\n" +
            "1/4 cup Green onion; chopped\n" +
            "1 large Vidalia onion; chopped\n" +
            "3 stalks Celery; chopped\n" +
            "1 tablespoon Garlic; minced\n" +
            "5 pounds Potato; peeled and chopped in medium-sized chunks\n" +
            "1/2 teaspoon Salt\n" +
            "1 tablespoon Chicken bouillon granules\n" +
            "8 ounces Ham; chopped into squares\n" +
            "3 cups Broccoli; frozen, chopped\n" +
            "1 cup Sour cream\n" +
            "2 tablespoons Flour\n" +
            "2 cups milk\n" +
            "Cheddar cheese; shredded for garnish\n" +
            "Bacon bits; garnish\n" +
            "Makes 8 Servings");
    values.put("Preparation", "Melt butter in a large pan on medium high heat. Saute green onion, vidalia onion, celery, and garlic until done. Add potatoes, salt, and boullion granules. Fill the pan with water to just cover the potatoes. Bring to a boil and cook for 10 minutes. Add the ham and broccoli. Cook another 10 minutes. Check the potatoes. They should be just done and easily pierced with a knife, but not falling apart. While the potatoes are cooking, mix the sour cream with the flour, then add the milk. Add this mixture to the pan all at once after the potatoes are done. Bring to a boil. The soup will thicken and be ready to serve in 3-5 minutes. Garnish with cheddar cheese and bacon bits for a delicious, hearty meal.");
    values.put("img", R.drawable.broccoliandhampotatosoup);
    db.insert("Recipe", "_id", values);

    values.put("Name", "Buttermilk Fried Chicken");
    values.put("Ingredients", "3/4 cup Buttermilk\n" +
            "1 tsp. Salt\n" +
            "1/4 tsp. Pepper\n" +
            "2 1/2 lb. chicken fryers\n" +
            "1 cup Flour\n" +
            "1 1/2 cup Vegetable oil\n" +
            "Makes 4 Servings");
    values.put("Preparation", "Crispy fried chicken soaked in buttermilk for delicious an old fashioned flavor.\n" +
            "\n" +
            "Combine buttermilk, salt, pepper and chicken in shallow dish. Refrigerate 20 minutes, turning once. Remove chicken and dredge in flour. Cook in hot oil (350F.), turning to brown both sides. Reduce heat to 275F. Cover and cook 25 minutes. Uncover and cook an additional 5 minutes until crispy.\n" +
            "\n" +
            "Note: Drain chicken on paper towels.");
    values.put("img", R.drawable.buttermilkfriedchicken);
    db.insert("Recipe", "_id", values);

    values.put("Name", "Garlic Parmesan Crusted\n" +
            " Pork Chops ");
    values.put("Ingredients", "4 1/2-inch thick Center-cut loin pork chops; (bone-in or boneless)\n" +
            "3/4 teaspoon Salt\n" +
            "1/2 teaspoon Black Pepper\n" +
            "1/2 teaspoon Dried rubbed sage\n" +
            "1/2 teaspoon Dried thyme\n" +
            "3/4 cups Parmesan cheese; freshly grated\n" +
            "2 large Eggs\n" +
            "1 tablespoon Vegetable oil\n" +
            "1 cup Dried seasoned bread crumbs; (I use Progresso garlic and herb)\n" +
            "Olive oil; (for shallow frying)\n" +
            "4 large cloves Garlic; peeled and chopped\n" +
            "Lemon wedges; for serving\n" +
            "Parsley sprigs; to garnish\n" +
            "Makes 4 Servings");
    values.put("Preparation", "Spread Parmesan cheese in a shallow dish. Beat eggs with 1 tablespoon vegetable oil in another shallow dish. Spread bread crumbs in a third shallow dish.\n" +
            "\n" +
            "Pat the pork chops dry with paper towels. Combine salt, pepper, sage, and thyme, and sprinkle generously over both sides of chops, pressing seasonings into meat with your fingers. Bread pork chops one at a time. Dredge seasoned chops in Parmesan cheese, coating both sides completely, patting to adhere. Then, using tongs, carefully dip both sides of chops in egg mixture, letting excess drip back into dish to ensure thin coating. Finally, dip both sides of chops in bread crumbs, pressing crumbs with fingers to form an even, cohesive coating. Place breaded chops on wire rack set over baking sheet, allow coating to dry for about 5 minutes. (The recipe can be done ahead to this point; cover chops loosely with wax paper and refrigerate for up to one hour.)\n" +
            "\n" +
            "Heat a large heavy-bottomed skillet over medium-low heat with enough olive oil to come halfway up the sides of the chops, about 1/4 cup. To flavor oil; stir in garlic, and cook until lightly browned. Remove garlic from skillet and discard or reserve for other uses. Increase heat to medium. When oil shimmers, add chops and fry 4 to 5 minutes on each side, until golden brown and crispy, or to an internal temperature between 150 and 160 degrees F. Drain on rack set over baking sheet, then transfer to a warm serving platter. Serve with lemon wedges and garnish with parsley sprigs.");
    values.put("img", R.drawable.garlicparmesancrustedporkchops);
    db.insert("Recipe", "_id", values);

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS recipe");
    onCreate(db);

}

}

DB_Parse.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;

public class DB_Parse extends Activity {
ImageView Im;
TextView tv_name, tv_ingredients, tv_preparation, id;
Gallery gallery;
ImageSwitcher imageSwitcher;
Integer[] imageIDs = new Integer[3];
int msg_im;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recipe);

    Intent iIdentification = getIntent();
    msg_im = iIdentification.getIntExtra("dataIM", 0);
    String msg_name = iIdentification.getStringExtra("dataName");
    String msg_ingredients = iIdentification.getStringExtra("dataIngredients");
    String msg_preparation = iIdentification.getStringExtra("dataPreparation");
    Im = (ImageView) findViewById(R.id.iv_detail);
    tv_name = (TextView) findViewById(R.id.tvName);
    tv_ingredients = (TextView) findViewById(R.id.tvIngredients);
    tv_preparation = (TextView) findViewById(R.id.tvPreparation);
    Im.setImageResource(msg_im);
    tv_name.setText(msg_name);
    tv_ingredients.setText(msg_ingredients);
    tv_preparation.setText(msg_preparation);
}
}

i have a list of special diets

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/LinearLayout2"
android:orientation="vertical"
android:background="@drawable/jbsbackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/dairyfree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textColor="#ffffff"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Dairy-Free"
        android:onClick="onClick"
        android:clickable="true"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:fontFamily="sans-serif"/>

    <View
        android:layout_width="120dp"
        android:layout_height="1dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/white"/>

    <TextView
        android:id="@+id/diabetic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:text="Diabetic"
        android:onClick="onClick"
        android:clickable="true"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:fontFamily="sans-serif"/>

    <View
        android:layout_width="100dp"
        android:layout_height="1dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/white"/>

    <TextView
        android:id="@+id/food_allergies"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:text="Food Allergies"
        android:onClick="onClick"
        android:clickable="true"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:fontFamily="sans-serif"/>

    <View
        android:layout_width="160dp"
        android:layout_height="1dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/white"/>

    <TextView
        android:id="@+id/glutenfree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:text="Gluten-Free"
        android:onClick="onClick"
        android:clickable="true"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:fontFamily="sans-serif"/>

    <View
        android:layout_width="130dp"
        android:layout_height="1dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/white"/>

    <TextView
        android:id="@+id/lowfat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:text="Low-Fat"
        android:onClick="onClick"
        android:clickable="true"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:fontFamily="sans-serif"/>

    <View
        android:layout_width="100dp"
        android:layout_height="1dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/white"/>

    <TextView
        android:id="@+id/lowcarb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:text="Low-Carb"
        android:onClick="onClick"
        android:clickable="true"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:fontFamily="sans-serif"/>

    <View
        android:layout_width="100dp"
        android:layout_height="1dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/white"/>

    <TextView
        android:id="@+id/lowsodium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:text="Low-Sodium"
        android:onClick="onClick"
        android:clickable="true"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:fontFamily="sans-serif"/>

    <View
        android:layout_width="140dp"
        android:layout_height="1dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/white"/>

    <TextView
        android:id="@+id/vegetarian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:text="Vegetarian"
        android:onClick="onClick"
        android:clickable="true"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:fontFamily="sans-serif"/>

    <View
        android:layout_width="140dp"
        android:layout_height="1dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/white"/>

    <TextView
        android:id="@+id/vegan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:text="Vegan"
        android:onClick="onClick"
        android:clickable="true"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:fontFamily="sans-serif"/>

    <View
        android:layout_width="100dp"
        android:layout_height="1dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:background="@android:color/white"/>

    <TextView
        android:id="@+id/nutfree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:text="Nut-Free"
        android:onClick="onClick"
        android:clickable="true"
        android:layout_gravity="center_horizontal"
        android:textStyle="bold"
        android:fontFamily="sans-serif"/>

</LinearLayout>
</ScrollView>

</LinearLayout>

Could someone help me in trying to make a code so that once i click on "Dairy-Free" or "Diabetic" etc a list of those type of recipes will appear on the next page

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire