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 List using the concrete type's Class
How to run tshark in Java to get packets in real-time?
Gradle FatJar task creating multiple MAINFEST.MF files
read Java doubles from binary file into Python [closed]
Why runtime.exec() is not executing. when I given sshpass command
How to create shared durable mailbox for remote actors in Akka?
How should I replace drawables?
Java - How do i make a string read and count amounts in another string (
why does spring-boot and postgres connection breaks after certain time?
Provide periodically updated image from server without corruption
Java Fork/Join for large inputs not finishing
uploaded image does not display until I refresh the page Spring Mvc
Why is this Mockito test failing?
Spring boot API which accepts JSON (mandatory) and multipart file (optional)
i need to seperate the string and integer and also to display it in seperate colomns
Unmarshal JSON to Java POJO in JAX-RS

Categories

HOME
openstack
image-processing
stock
paw-app
callback
fancybox
onelogin
applepay
kentor-authservices
resize
timeout
orchardcms
workload-scheduler
pc
lucene.net
nas
cultureinfo
lcd
sqlcipher
cas
user-interaction
ping
replaceall
kendo-datasource
tasklet
react-chartjs
titanium-mobile
http-get
protovis
mmenu
fusionpbx
wpf-controls
plsql-psp
restlet
disassembling
scaffold
retina-display
uft-api
opshub
jvm-languages
midl
outlook-2013
xml-documentation
zip4j
clean-architecture
nativeapplication
knockout-components
crypt
capacity
account-kit
word-vba-mac
passport-google-oauth
intrusion-detection
gstreamer-0.10
lua-5.1
qcustomplot
hsv
kendonumerictextbox
rtbkit
srand
jms2
rgeo
ionic
twirl
arcanist
unity3d-gui
fscommand
jscript.net
file-copying
heisenbug
marmalade
hyprlinkr
resty-gwt
viadeo
eclipse-memory-analyzer
gridcontrol
seed
type-equivalence
drools-planner
gil
frameset
dsn
tridion-worldserver
onsubmit
chrono
buildr
hgsubversion
cufon
gnustep
subgurim-maps
locationlistener
table-footer
coda-slider
.nettiers
dmx512
meego
kdbg
adrotator
pascal-fc

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