java


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.
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();
...
}

Related Links

key - value mapping in a HashMap
Why are my radiobuttons not mutually exclusive?
Surrounding JTextPane with images
How can I get a vector output?
PriorityBlockingQueue: is it realy blocking
Assigning an Integer to an Integer in java [closed]
Building a data model using kmeans algorithm and using it to classify new data set
Application stopped working when loading layout with setContentView
Renderer won't render texture
Java/Android - set DecimalFormat to show only mantissa
ClassCastException when unmarshalling JSON
Reading data from HBase in Reducer
re-using the com.google.gson.Gson object in a service or servlet?
Eclipse keyboard shortcut for passing a parameter to a method
Android - ListView show empty
Swipe + tabs android Class Cast Exception

Categories

HOME
gitlab
asp.net-core
image-processing
deezer
fluentd
tizen
stock
tesseract
react-router
maven-3
sql-server-2016
blueprintjs
yum
gnupg
reveal.js
windows-server-2012
circuit
caml
foselasticabundle
apache-metamodel
nas
ml
excel-2007
captiveportal
sparse-matrix
facebook-access-token
bootstrap-duallistbox
social-media
fifo
saas
tasklet
.net-4.0
siesta-swift
hybridauth
dosbox
simplexml
serve
qwerty
automake
hue
preconditions
g-code
reportingservices-2005
nxlog
eclipse-gef
qcombobox
btrace
apache-fop
multipeer-connectivity
carrot
kbuild
python-cryptography
color-profile
titanium-android
or-tools
probability-density
log4c
mikroc
markojs
nsviewcontroller
system.management
ford-fulkerson
javax.sound.midi
thredds
qdialog
dereference
camanjs
p4java
jquery-layout
reactfx
valuechangelistener
hippomocks
mcts
easy-install
mhtml
word-processor
manchester-syntax
propertyeditor
enter
pendrive
netdna-api
blitz++
dmx512
gin
firefox-5
private-members

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App