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

Creating a for loop to double the previous value
Hashmap hashtable linkedHashmap what to use?
Using `#ConfigurationProperties` annotation on `#Bean` Method
Java downcast objects invoking sub method [duplicate]
Deserializing json that is false or object with Retrofit GsonConverterFactory
Jackson unmarshal: store inner xml as string
Remove Method for a Single and Double Linked List (Java)
What is the size in memory of a Clojure keyword?
Giving priority to Button press on a Dynamically Updating UI in Android
Change the font of titled border in java swing [duplicate]
how to use method on specific object from arraylist
Java Lock explanation
If else not working as expected
Caused by: java.lang.IllegalStateException: The specified child already has a parent
Angular2 http.get from Restful Webservice when i run this below code i am getting error status 200, URL null
Request getting overridden in multi request environment for filters

Categories

HOME
hive
gremlin
fft
xmpp
cmd
rubygems
retrofit
serverspec
datastax-java-driver
bs4
cross-validation
python-unittest
tomcat6
export-to-csv
clearcase-ucm
django-simple-history
systemc
fatal-error
transformation
jsdoc
invantive-sql
samoa
viewport
alpine
textfield
lldb
nas
amazonsellercentral
cx-freeze
clickonce
smb
pepper
minitab
bosh
xquery-3.0
data-manipulation
caret
protovis
wpf-controls
twitch
eigenvalue
isbn
mime
s
menuitem
jsch
parentheses
rdfs
svmlight
nstextview
nbconvert
vtigercrm
cubes
nxt
fody
django-debug-toolbar
paypal-express
dlna
ipconfig
system.management
energy
browser-link
jfugue
ios8-today-widget
google-places
pundit
muse
drawbitmap
sonarqube5.1.2
angularjs-ng-click
cdt
qcodo
castle
google-reader
article
jubula
commoncrypto
dir
soundtouch
dbconnection
batterylevel
kyotocabinet
online-compilation
work-stealing
f#-powerpack
winbugs14
mongomapper
jmock
mysql-error-1005
modelstate
bespin
ncqrs
fixed-width
kdbg
text-coloring
zune
wsdl.exe

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