Mateo Mateo - 2 months ago 14
Android Question

Using AsyncTask with SQLite Database

I need to use AsyncTask to perform database updates in my application (add and delete mainly). I don't know what exactly to code for that function in my program. It's going to go in my second activity file.

DatabaseActivity.java

package com.example.healthylife;



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){
MyDBHandler dbHandler = new MyDBHandler (this, null, null, 1);

Recipe recipe = new Recipe();

dbHandler.addRecipe(recipe);
RecipeBox.setText("");
CategoryBox.setText("");
IngredientsBox.setText("");
InstructionsBox.setText("");
}

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){
MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1);

boolean result = dbHandler.deleteRecipe(RecipeBox.getText().toString());

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

}


Any help would be appreciated. Thanks.

Answer

Your question is a very broad. I think, you dont need an additional Activity to perform that operation.

Create an Async task in the same activity with an constructor taking the database connection handler,the data to be updated, and a flag indicating its an update or delete operation. You will have some callback function in your activity(like when the user press "save" or "delete"). Upon this trigger, start an async task with the respective values.

In your asynctask class do the operations what you have coded above respectively.

You may write separate async tasks for each of the operations! It depends on your design!

private class AsyncTaskSaveDelete extends AsyncTask<Void, Void, boolean> {
    private DBHandler handler;
    private Recipe recipe;
    private boolean isDelete;

    public AsyncTaskSaveDelete(DBHandler handler,Recipe recipe,boolean isDelete ){
        this.handler = handler;
        this.recipe = recipe;
        this.isDelete = isDelete;
    }

     protected Long doInBackground( {
         if(isDelete){
            return dbHandler.deleteRecipe(recipe);
         }else{
          return dbHandler.addRecipe(recipe);
         }
     }

     protected void onPostExecute(boolean result) {
         runOnUiThread(new PostRunnable(){
            //update the UI
         })
     }
 }


public void newRecipe (View view){
...
    new AsyncTaskSaveDelete(handler,recipe,false).execute();
...
}