If you think the Android project 2014-Droid-code listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package cs534.sample.dbAsyncTask;
//fromwww.java2s.comimport android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/*
* AddStudent.class
*
* This Activity is a UI used simply to add records to the db
*
* It uses a DBHelper class for the sqlite database
*/publicclass AddStudent extends Activity implements OnClickListener {
private Context context;
private DBHelper dbh;
TextView status;
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
context = getApplicationContext();
Button btn_submit = (Button)findViewById(R.id.btn_submit);
btn_submit.setOnClickListener(this);
status = (TextView)findViewById(R.id.tv_status);
}
publicvoid onClick(View v) {
dbh = DBHelper.getDBHelper(this);
// get the data from the UI
String firstName = ((EditText)findViewById(R.id.et_name)).getText().toString();
String lastName = ((EditText)findViewById(R.id.et_lastName)).getText().toString();
String className = ((EditText)findViewById(R.id.et_class)).getText().toString();
String grade = ((EditText)findViewById(R.id.et_grade)).getText().toString();
// add the data to the db
InsertDataTask indata = new InsertDataTask();
indata.execute(firstName, lastName, className, grade);
} //onClick()
privateclass InsertDataTask extends AsyncTask<String, Void, Long> {
privatefinal ProgressDialog dialog = new ProgressDialog(AddStudent.this);
protectedvoid onPreExecute() {
this.dialog.setMessage("Inserting data...");
this.dialog.show();
}
protected Long doInBackground(String... args) {
return dbh.insertNewStudent(args[0], args[1],args[2],Integer.parseInt(args[3]));
// insertNewStudent(firstName, lastName, className, grade);;
}
protectedvoid onPostExecute(Long retcode) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (retcode != -1) {
Toast.makeText(context, R.string.insert_ok, Toast.LENGTH_LONG).show();
status.setText(R.string.insert_ok);
} else
status.setText(R.string.insert_fail);
}
} // InsertDataTask class (AsyncTask)
} // Activity class