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

Java Swing: Custom Cell Editor does not return most recent value
Remember Me, Single Session and Encrypt Cookie with HTTP (Not HTTPS)
Replace old fragment to add new fragment inside viewpager
Can javascript be used to render web pages
can we use super keyword within the main() class in java? [duplicate]
env.getProperty not working Spring PropertyPlaceholderConfigurer
ExceptionBug in java project?
Android app with onClickListener not working
Trouble with using UNICODE symbols in Java
How to delete in one time few buttons or text fields?
SimpleJDBCCall class parameter passing
Avoid Java compiler optimization of a loop and an if statement
I have a string variable and another string assigned to same variable. Then how to get old string?
I want to compare two versions of data from a table using JAVA and display the differences on a UI.What can be the best way to approach this?
Is there any sense in access modifiers for fields of the private inner class?
SQLite problems in Android, “no such table” error

Categories

HOME
zeromq
tizen
smarty
relayjs
react-router
analysis
spring-jdbc
yarn
paw-app
sql-server-2016
ojdbc
yum
ios-charts
modx-revolution
google-apps-marketplace
flask-wtforms
foselasticabundle
seaborn
saxon
footer
openedx
rundeck
shopware
buildbot
url-scheme
applozic
google-cloud-nl
phonegap
karaf
protovis
phpfreechat
repo
catalog
gulp-sourcemaps
spring-security-kerberos
nxlog
netcdf4
theming
ionicons
aurelia-validation
tactic
libusb-win32
pillow
memory-alignment
color-profile
topbeat
root-framework
heidisql
removechild
savon
multi-level
google-web-starter-kit
nsfilemanager
ubuntu-10.04
actionbardrawertoggle
packagist
php-parse-error
maven-tomcat-plugin
wireshark-dissector
riak-cs
cakephp-3.1
iiviewdeckcontroller
apache-commons-net
system.reflection
python-green
dia
tween
angularjs-ng-click
relocation
wordpress-theme-customize
xcode-6.2
websocket4net
preferences
onactivityresult
stxxl
geos
aqtime
tidy
limejs
clipper
pvrtc
sublist
rdoc
labwindows
gwt-ext
servicehost
iphone-web-app
yslow
sudzc
blitz++
gamequery
gamma
dentrix
jquery-ui-droppable
uimenucontroller
pascal-fc
thread-local-storage
lzh

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