2016-09-21 11 views
0

このプログラムは、SQLiteデータベースを使用してAsyncTaskを実行して保存されたユーザーが追加したレシピを格納する過去数週間にわたって開発しています。レシピは追加や削除が可能ですが、AsyncTaskを使用して特定のカテゴリに入力されたレシピを表示する必要があります。たとえば、ユーザーが「パン」レシピを追加すると、「パン」ボタンをクリックしたときにホーム画面にレシピが表示されます。ここで私はこれまで持っているものです。SQLiteデータベースからAsyncTaskを介してデータを取得して表示する

MyDBHandler.java:

package com.example.healthylife; 

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


public class MyDBHandler extends SQLiteOpenHelper { 

private static final int DATABASE_VERSION = 1; 
private static final String DATABASE_NAME = "recipeDB.db"; 
private static final String TABLE_RECIPE = "recipe"; 

private static final String COLUMN_ID = "_id"; 
private static final String COLUMN_RECIPENAME = "_recipename"; 
private static final String COLUMN_CATEGORY = "_category"; 
private static final String COLUMN_INGREDIENTS = "_ingredients"; 
private static final String COLUMN_INSTRUCTIONS = "_instructions"; 

public MyDBHandler(Context context, String name, 
        SQLiteDatabase.CursorFactory factory, int version){ 
    super(context, DATABASE_NAME, factory, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db){ 
    String CREATE_RECIPE_TABLE = "CREATE TABLE " + TABLE_RECIPE + "(" + 
      COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_RECIPENAME + " TEXT," + 
      COLUMN_CATEGORY + " TEXT," + COLUMN_INGREDIENTS + " TEXT," + 
      COLUMN_INSTRUCTIONS + " TEXT" + ")"; 
    db.execSQL(CREATE_RECIPE_TABLE); 
} 

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

public void addRecipe(Recipe recipe){ 
    ContentValues values = new ContentValues(); 
    values.put(COLUMN_RECIPENAME, recipe.getRecipeName()); 
    values.put(COLUMN_CATEGORY, recipe.getCategory()); 
    values.put(COLUMN_INGREDIENTS, recipe.getIngredients()); 
    values.put(COLUMN_INSTRUCTIONS, recipe.getInstructions()); 

    SQLiteDatabase db = this.getWritableDatabase(); 
    db.insert(TABLE_RECIPE, null, values); 
    db.close(); 
} 

public Recipe findRecipe(String recipename){ 
    String query = "Select * FROM " + TABLE_RECIPE + "WHERE " + COLUMN_RECIPENAME + " = \"" + 
      recipename + "\""; 

    SQLiteDatabase db = this.getWritableDatabase(); 

    Cursor cursor = db.rawQuery(query, null); 

    Recipe recipe = new Recipe(); 

    if (cursor.moveToFirst()){ 
     cursor.moveToFirst(); 
     recipe.setID(Integer.parseInt(cursor.getString(0))); 
     recipe.setRecipeName(cursor.getString(1)); 
     recipe.setCategory(cursor.getString(2)); 
     recipe.setIngredients(cursor.getString(3)); 
     recipe.setInstructions(cursor.getString(4)); 
     cursor.close(); 
    }else{ 
     recipe = null; 
    } 
    db.close(); 
    return recipe; 
} 

public boolean deleteRecipe(String recipename) { 
    boolean result = false; 

    String query = "Select * FROM " + TABLE_RECIPE + " WHERE " + COLUMN_RECIPENAME + " = \"" + 
      recipename + "\""; 

    SQLiteDatabase db = this.getWritableDatabase(); 

    Cursor cursor = db.rawQuery(query, null); 

    Recipe recipe = new Recipe(); 

    if (cursor.moveToFirst()) { 
     recipe.setID(Integer.parseInt(cursor.getString(0))); 
     db.delete(TABLE_RECIPE, COLUMN_ID + " = ?", 
       new String[] { String.valueOf(recipe.getID()) }); 
     cursor.close(); 
     result = true; 
    } 
    db.close(); 
    return result; 
} 

public Recipe getBread(String recipename) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    String query = "Select * FROM " + TABLE_RECIPE + "WHERE " + COLUMN_RECIPENAME + " = \"" + 
      recipename + "\""; 
    Cursor cursor = db.rawQuery(query, null); 

    Recipe recipe = new Recipe(); 
    if (cursor != null) { 
     cursor.moveToFirst(); 
     recipe.setID(Integer.parseInt(cursor.getString(0))); 
     recipe.setRecipeName(cursor.getString(1)); 
     recipe.setCategory(cursor.getString(2)); 
     recipe.setIngredients(cursor.getString(3)); 
     recipe.setInstructions(cursor.getString(4)); 
     cursor.close(); 
    }else{ 
     recipe = null; 
    } 
    db.close(); 
    return recipe; 
} 

public Recipe getFruit(String recipename) { 
    SQLiteDatabase db = this.getReadableDatabase(); 

    String query = "Select * FROM " + TABLE_RECIPE + "WHERE " + COLUMN_RECIPENAME + " = \"" + 
      recipename + "\""; 
    Cursor cursor = db.rawQuery(query, null); 

    Recipe recipe = new Recipe(); 
    if (cursor != null) { 
     cursor.moveToFirst(); 
     recipe.setID(Integer.parseInt(cursor.getString(0))); 
     recipe.setRecipeName(cursor.getString(1)); 
     recipe.setCategory(cursor.getString(2)); 
     recipe.setIngredients(cursor.getString(3)); 
     recipe.setInstructions(cursor.getString(4)); 
     cursor.close(); 
    }else{ 
     recipe = null; 
    } 
    db.close(); 
    return recipe; 
} 

public Recipe getVeg(String recipename) { 
    SQLiteDatabase db = this.getReadableDatabase(); 

    String query = "Select * FROM " + TABLE_RECIPE + "WHERE " + COLUMN_RECIPENAME + " = \"" + 
      recipename + "\""; 
    Cursor cursor = db.rawQuery(query, null); 

    Recipe recipe = new Recipe(); 
    if (cursor != null) { 
     cursor.moveToFirst(); 
     recipe.setID(Integer.parseInt(cursor.getString(0))); 
     recipe.setRecipeName(cursor.getString(1)); 
     recipe.setCategory(cursor.getString(2)); 
     recipe.setIngredients(cursor.getString(3)); 
     recipe.setInstructions(cursor.getString(4)); 
     cursor.close(); 
    }else{ 
     recipe = null; 
    } 
    db.close(); 
    return recipe; 
} 

public Recipe getSoup(String recipename) { 
    SQLiteDatabase db = this.getReadableDatabase(); 

    String query = "Select * FROM " + TABLE_RECIPE + "WHERE " + COLUMN_RECIPENAME + " = \"" + 
      recipename + "\""; 
    Cursor cursor = db.rawQuery(query, null); 

    Recipe recipe = new Recipe(); 
    if (cursor != null) { 
     cursor.moveToFirst(); 
     recipe.setID(Integer.parseInt(cursor.getString(0))); 
     recipe.setRecipeName(cursor.getString(1)); 
     recipe.setCategory(cursor.getString(2)); 
     recipe.setIngredients(cursor.getString(3)); 
     recipe.setInstructions(cursor.getString(4)); 
     cursor.close(); 
    }else{ 
     recipe = null; 
    } 
    db.close(); 
    return recipe; 
} 

DatabaseActivityクラスが追加を使用し、AsyncTaskの関数を削除

[Recipe.javaはゲッター/セッターを持っている]:

package com.example.healthylife; 



import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 


public class DatabaseActivity extends AppCompatActivity { 


TextView idView; 
EditText RecipeBox; 
EditText CategoryBox; 
EditText IngredientsBox; 
EditText InstructionsBox; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_database); 

    idView = (TextView) findViewById(R.id.Recipe_ID); 
    RecipeBox = (EditText) findViewById(R.id.edit_RecipeName); 
    CategoryBox = (EditText) findViewById(R.id.input_category); 
    IngredientsBox = (EditText) findViewById(R.id.edit_Ingredients); 
    InstructionsBox = (EditText) findViewById(R.id.edit_Instructions); 
} 

public void newRecipe (View view){ 

    AsyncTaskSave op = new AsyncTaskSave(); 
    op.execute(); 

} 

public void lookupRecipe (View view){ 
    MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1); 

    Recipe recipe = dbHandler.findRecipe(RecipeBox.getText().toString()); 

    if (recipe != null){ 
     idView.setText(String.valueOf(recipe.getID())); 

     CategoryBox.setText(String.valueOf(recipe.getCategory())); 
     IngredientsBox.setText(String.valueOf(recipe.getIngredients())); 
     InstructionsBox.setText(String.valueOf(recipe.getInstructions())); 
    }else { 
     idView.setText("No Match Found"); 
    } 
} 

public void removeRecipe (View view){ 

    AsyncTaskDelete task = new AsyncTaskDelete(); 
    task.execute(); 
} 

private class AsyncTaskSave extends AsyncTask <Object, Object, String> { 

    @Override 
    protected String doInBackground(Object...objects){ 
     MyDBHandler dbHandler = new MyDBHandler (DatabaseActivity.this, null, null, 1); 
     Recipe recipe = new Recipe(); 
     dbHandler.addRecipe(recipe); 

     return null; 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     Recipe recipe = new Recipe(); 
     RecipeBox.setText(String.valueOf(recipe.getRecipeName())); 
     CategoryBox.setText(String.valueOf(recipe.getCategory())); 
     IngredientsBox.setText(String.valueOf(recipe.getIngredients())); 
     InstructionsBox.setText(String.valueOf(recipe.getInstructions())); 

     result = "Added Successfully"; 
     idView.setText(result); 
    } 
} 

private class AsyncTaskDelete extends AsyncTask<Object, String, String>{ 
    @Override 
    protected String doInBackground(Object...objects){ 
     MyDBHandler dbHandler = new MyDBHandler (DatabaseActivity.this, null, null, 1); 
     dbHandler.deleteRecipe(null); 

     return null; 
    } 

    @Override 
    protected void onPostExecute(String result){ 

     if (result == null){ 
      idView.setText("Record Deleted"); 
      RecipeBox.setText(""); 
      CategoryBox.setText(""); 
      IngredientsBox.setText(""); 
      InstructionsBox.setText(""); 
     }else 
      idView.setText("No Match Found"); 
    } 
} 

} 

SyncActivity.javaクラスは、DatabaseActivityクラスによって追加されたレシピを表示し、メイン画面のボタンが前にあるときにそれらを呼び出しますssed:今ここに

package com.example.healthylife; 

import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 

public class SyncActivity extends AppCompatActivity{ 

TextView idView; 
TextView RecipeBox; 
TextView CategoryBox; 
TextView IngredientsBox; 
TextView InstructionsBox; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sync); 

    idView = (TextView) findViewById(R.id.Recipe_ID); 
    RecipeBox = (TextView) findViewById(R.id.edit_RecipeName); 
    CategoryBox = (TextView) findViewById(R.id.input_category); 
    IngredientsBox = (TextView) findViewById(R.id.edit_Ingredients); 
    InstructionsBox = (TextView) findViewById(R.id.edit_Instructions); 
} 

public void getBread (View view) { 

    AsyncTaskBread os = new AsyncTaskBread(); 
    os.execute(); 
} 

public void getFruit (View view){ 
    AsyncTaskFruit ob = new AsyncTaskFruit(); 
    ob.execute(); 
} 

public void getVeg (View view){ 
    AsyncTaskVeg ow = new AsyncTaskVeg(); 
    ow.execute(); 
} 

public void getSoup (View view){ 
    AsyncTaskSoup ox = new AsyncTaskSoup(); 
    ox.execute(); 
} 

private class AsyncTaskBread extends AsyncTask <Object, Object, String>{ 

    MyDBHandler dbHandler; 

    @Override 
    protected String doInBackground(Object...objects){ 
     MyDBHandler dbHandler = new MyDBHandler (SyncActivity.this, null, null, 1); 
     dbHandler.getBread("Bread"); 
     return "Bread"; 
    } 

    @Override 
    protected void onPostExecute(String result){ 
     Recipe recipe = dbHandler.getBread(CategoryBox.getText().toString()); 

     if(recipe != null){ 
      idView.setText(String.valueOf(recipe.getID())); 
      RecipeBox.setText(String.valueOf(recipe.getRecipeName())); 
      IngredientsBox.setText(String.valueOf(recipe.getIngredients())); 
      InstructionsBox.setText(String.valueOf(recipe.getInstructions())); 
     }else { 
      result = "No Match Found"; 
      idView.setText(result); 
     } 
    } 
} 

private class AsyncTaskFruit extends AsyncTask <Object, Object, String>{ 

    MyDBHandler dbHandler; 

    @Override 
    protected String doInBackground(Object...objects){ 
     MyDBHandler dbHandler = new MyDBHandler (SyncActivity.this, null, null, 1); 
     dbHandler.getFruit("Fruit"); 
     return "Fruit"; 

    } 

    @Override 
    protected void onPostExecute(String result){ 
     Recipe recipe = dbHandler.getFruit(CategoryBox.getText().toString()); 

     if(recipe != null){ 
      idView.setText(String.valueOf(recipe.getID())); 
      RecipeBox.setText(String.valueOf(recipe.getRecipeName())); 
      IngredientsBox.setText(String.valueOf(recipe.getIngredients())); 
      InstructionsBox.setText(String.valueOf(recipe.getInstructions())); 
     }else { 
      result = "No Match Found"; 
      idView.setText(result); 
     } 
    } 
} 

private class AsyncTaskVeg extends AsyncTask <Object, Object, String>{ 

    MyDBHandler dbHandler; 

    @Override 
    protected String doInBackground(Object...objects){ 
     MyDBHandler dbHandler = new MyDBHandler (SyncActivity.this, null, null, 1); 
     dbHandler.getVeg("Vegetables"); 
     return "Vegetables"; 

    } 

    @Override 
    protected void onPostExecute(String result){ 
     Recipe recipe = dbHandler.getVeg(CategoryBox.getText().toString()); 

     if(recipe != null){ 
      idView.setText(String.valueOf(recipe.getID())); 
      RecipeBox.setText(String.valueOf(recipe.getRecipeName())); 
      IngredientsBox.setText(String.valueOf(recipe.getIngredients())); 
      InstructionsBox.setText(String.valueOf(recipe.getInstructions())); 
     }else { 
      result = "No Match Found"; 
      idView.setText(result); 
     } 
    } 
} 

private class AsyncTaskSoup extends AsyncTask <Object, Object, String>{ 

    MyDBHandler dbHandler; 

    @Override 
    protected String doInBackground(Object...objects){ 
     MyDBHandler dbHandler = new MyDBHandler (SyncActivity.this, null, null, 1); 
     dbHandler.getSoup("Soup"); 
     return "Soup"; 

    } 

    @Override 
    protected void onPostExecute(String result){ 
     Recipe recipe = dbHandler.getSoup(CategoryBox.getText().toString()); 

     if(recipe != null){ 
      idView.setText(String.valueOf(recipe.getID())); 
      RecipeBox.setText(String.valueOf(recipe.getRecipeName())); 
      IngredientsBox.setText(String.valueOf(recipe.getIngredients())); 
      InstructionsBox.setText(String.valueOf(recipe.getInstructions())); 
     }else { 
      result = "No Match Found"; 
      idView.setText(result); 
     } 
    } 
} 
} 

は、ホーム画面です:

Home Screen in app

レシピはレシピ帳に追加された後、各ボタンをクリックすると、彼らは特定の分野の詳細を表示する代わりに、私のすべきですフィールドは空です:

Recipe call when buttons are clicked

は今SyncActivityクラスはVを取得する必要がありますDatabaseActivityクラスを介して入力されたデータベースの値ですが、そうではありません。 myDBHandlerのコードで変更が必要なことはありますか?それともSyncActivityにありますか?

答えて

0

問題は、AsyncTaskBreadクラスのプライベートオブジェクトdbHandlerは決して初期化されないということです。 dbHandlerをdoInBackgroundで初期化しますが、そのオブジェクトはその関数内にのみ存在します。 MyDBHandler dbHandler = new MyDBHandler (SyncActivity.this, null, null, 1);dbHandler = new MyDBHandler (SyncActivity.this, null, null, 1);に変更してください。onPostExecuteはUI(メイン)スレッドで実行されるため、SQLクエリをonPostExecuteからdoInBackgroundに移動することをお勧めします。

+0

私の答えがあなたを助けてくれたら、アップボートして、私の答えを受け入れたとマークしてください。 –

関連する問題