Hello Android friends… This is going to be my first blog post for the Dooms Year (2012)
By the way, I have to keep a promise although I wanted to write a new topic. Therefore I am continuing with the SQLite post series and today I am going to show you how to do Update and Delete functions.
In my previous posts I have shown you Insert and Retrieve functions. So the interfaces are not going to change a lot except for new Buttons for Update, Delete and going back to previous interface.
According to my procedure, to update an item you have to click on that particular item from the list. Then you will be redirect to an interface where you can edit details of that particular item that you have clicked. First figure shows you the story till that moment.
After you edit the values in the EditText fields, click on the Update button. Then you will redirect to the first interface in our simple program where we show the Undergraduates’ names. If you have updated the Undergraduate name, then you will be able to see the changes in the ListView also. If not click on the same item and you can see the updated details as in the following figure.
By pressing the Cancel button you can go to the previous interface where the Undergraduates’ names list visible.
Deleting is also the same. First you click on the item you want to delete. Then it will redirect you to the interface where you can see all the details of the clicked item. Then by pressing the Delete button you can delete the selected item with all it’s details.
All above I have described about the front end. Here is the back end code with detailed comments. Please copy the source in to an IDE where you can clearly differentiate comments from the code and read the comments to get the idea of that particular code sample.
package com.anuja.sqlite;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class UpdateDeleteUndergraduateActivity extends Activity implements OnClickListener {
private EditText uGraduateNameEditText;
private EditText uGraduateUniIdEditText;
private EditText uGraduateGpaEditText;
private Button cancelButton;
private Button updateButton;
private Button deleteButton;
private String bundledUgraduateName;
private String bundledUgraduateUniId;
private String bundledUgraduateGpa;
private Double bundleUgraduateGpaDouble;
private String ugraduateNameEditTextValue;
private String ugraduateUniIdEditTextValue;
private Double ugraduateGpaEditTextValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update_delete_ugraduate);
uGraduateNameEditText = (EditText) findViewById(R.id.updateDelateUgraduateNameEditText);
uGraduateUniIdEditText = (EditText) findViewById(R.id.updateDeleteUgraduateUniIdEditText);
uGraduateGpaEditText = (EditText) findViewById(R.id.updateDeleteUgaduateGpaEditText);
cancelButton = (Button) findViewById(R.id.updateDeleteUgraduate_cancel_button);
cancelButton.setOnClickListener(this);
updateButton = (Button) findViewById(R.id.updateDeleteUgraduate_update_button);
updateButton.setOnClickListener(this);
deleteButton = (Button) findViewById(R.id.updateDeleteUgraduate_delete_button);
deleteButton.setOnClickListener(this);
Bundle takeBundledData = getIntent().getExtras();
// First we need to get the bundle data that pass from the UndergraduateListActivity
bundledUgraduateName = takeBundledData.getString("clickedUgraduateName");
bundledUgraduateUniId = takeBundledData.getString("clickedUgraduateUniId");
// setText method ask for a String value
//But getDouble method returns a Double value
bundleUgraduateGpaDouble = takeBundledData.getDouble("clickedUgraduateGpa");
//So we need to convert that Double value to String value
bundledUgraduateGpa = Double.toString(bundleUgraduateGpaDouble);
// Set the values that we extracted from the Bundle in the EditText fields
uGraduateNameEditText.setText(bundledUgraduateName);
uGraduateUniIdEditText.setText(bundledUgraduateUniId);
uGraduateGpaEditText.setText(bundledUgraduateGpa);
}
@Override
public void onClick(View v) {
// We need to update or delete details which is in the EditText fields after user edit the values
// These values are the ContentValues that we are going to use in future
ugraduateNameEditTextValue = uGraduateNameEditText.getText().toString();
ugraduateUniIdEditTextValue = uGraduateUniIdEditText.getText().toString();
String ugraduateGpaEditTextValueStr = uGraduateGpaEditText.getText().toString();
ugraduateGpaEditTextValue = Double.valueOf(ugraduateGpaEditTextValueStr);
// It is easy to set values to the POJO class and pass the class instance to the updateUgraduateDetails() method
UndergraduateDetailsPojo undergraduateDetailsPojo = new UndergraduateDetailsPojo();
undergraduateDetailsPojo.setuGraduateName(bundledUgraduateName);
undergraduateDetailsPojo.setuGraduateUniId(bundledUgraduateUniId);
// POJO class ask for a double value for the GPA
//So we have to convert the EditText value to Double value
Double ugraduateGpaDoubleValue = Double.parseDouble(bundledUgraduateGpa);
undergraduateDetailsPojo.setuGraduateGpa(ugraduateGpaDoubleValue);
if(v.getId() == R.id.updateDeleteUgraduate_cancel_button){
finish();
}else if(v.getId() == R.id.updateDeleteUgraduate_update_button){
updateUgraduateDetails(undergraduateDetailsPojo);
}else if(v.getId() == R.id.updateDeleteUgraduate_delete_button){
deleteUgraduateDetails(undergraduateDetailsPojo);
}
}
private void updateUgraduateDetails(UndergraduateDetailsPojo undergraduateDetailsPojo) {
AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();
// ContentValues class is used to store a set of values
//It is like name-value pairs
// "value" part contains the values that we are going to UPDATE
ContentValues contentValues = new ContentValues();
contentValues.put(AndroidOpenDbHelper.COLUMN_NAME_UNDERGRADUATE_NAME, ugraduateNameEditTextValue);
contentValues.put(AndroidOpenDbHelper.COLUMN_NAME_UNDERGRADUATE_UNI_ID, ugraduateUniIdEditTextValue);
contentValues.put(AndroidOpenDbHelper.COLLUMN_NAME_UNDERGRADUATE_GPA, ugraduateGpaEditTextValue);
// If we are using multiple whereClauseArguments, array size should have to change
String[] whereClauseArgument = new String[1];
whereClauseArgument[0] = undergraduateDetailsPojo.getuGraduateName();
System.out.println("whereClauseArgument[0] is :" + whereClauseArgument[0]);
/**
* This is the normal SQL query for UPDATE
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
*/
sqliteDatabase.update(AndroidOpenDbHelper.TABLE_NAME_GPA, contentValues, AndroidOpenDbHelper.COLUMN_NAME_UNDERGRADUATE_NAME+"=?", whereClauseArgument);
// For two whereClauseArguments
//sqliteDatabase.update(AndroidOpenDbHelper.TABLE_NAME_GPA, contentValues, BaseColumns._ID+"=? AND name=?", whereClauseArgument);
sqliteDatabase.close();
finish();
}
private void deleteUgraduateDetails(UndergraduateDetailsPojo deleteUndergraduateDetailsPojo) {
AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();
String[] whereClauseArgument = new String[1];
whereClauseArgument[0] = deleteUndergraduateDetailsPojo.getuGraduateName();
// Only difference between UPDATE and DELETE is
//DELETE does not have ContentValues part
sqliteDatabase.delete(AndroidOpenDbHelper.TABLE_NAME_GPA, AndroidOpenDbHelper.COLUMN_NAME_UNDERGRADUATE_NAME+"=?", whereClauseArgument);
sqliteDatabase.close();
finish();
}
}
That is the end of this post. As I have promised to you all here is the link to download source from the GitHub.
Thank you for staying with me


please upload the full source code.
can you please provide a full source code m gettign errors
plzzzzz plz plz plz
Hey,
I have uploaded the source to GitHub and I have also provided the link in my post. Please see the lase paragraph of the post.
Good luck…!!!
could you please provide the XML file or upload it in the same link in GITHUB , that will be of great help for me. Thanks in advance.
Hey,
You have provided the java files but not the xml files.
Hi Aqeela,
That is true. You can create XML files by drag and drop. That’s why I have uploaded the screen shots of every Activity. There is nothing big in those XML files.
can u send me the xml files to me, i m getting errors plsssssssssss. My email id Diwakar.ds13@gmail.com
plsssssssssss do it
Thanks for such a awesome Explanation
good evening anuja ……. i have used your code… data inserting is working …. but data retrieving is not working properly…. instead of names only one variable is appearing that we hav used in code i e graduate name . in update and delete case even its not moving to next page after clicking on item …. have you used simple list view or customised list view with row.xml …..pls pls pls guide me …. what is the possible problem
Hey Anuj… Thanks for postin this tutorial.. I really appreciate your effort in compliling this tutorial.
thanks anuja
No problem… Poonam
can u send me the xml files to me, i m getting errors pls. My email id:niyamathdace@gmail.com
I wish you could post the entire project with the supporting xml files.
where are the xml files?
Hi,,
I am getting errors while executing this project.. pls send me xml files for this project
my email is: smran21@gmail.com
hi ,
when i try to salve anything in database, i get errors. Can u send me de full source?
thanks.
vinicius.staff@gmail.com
hi…..Can u send me full source code…….
kemelhassen@gmail.com
please xml
Please provide the XML files
You sent full source code to me pls T_T
umbroya@gmail.com
03-23 12:02:45.229: D/AndroidRuntime(333): Shutting down VM
03-23 12:02:45.229: W/dalvikvm(333): threadid=1: thread exiting with uncaught exception (group=0×40015560)
03-23 12:02:45.249: E/AndroidRuntime(333): FATAL EXCEPTION: main
03-23 12:02:45.249: E/AndroidRuntime(333): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.tasksfull/com.example.tasksfull.AddNewUndergraduateActivity}; have you declared this activity in your AndroidManifest.xml?
03-23 12:02:45.249: E/AndroidRuntime(333): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
03-23 12:02:45.249: E/AndroidRuntime(333): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
03-23 12:02:45.249: E/AndroidRuntime(333): at android.app.Activity.startActivityForResult(Activity.java:2827)
03-23 12:02:45.249: E/AndroidRuntime(333): at android.app.Activity.startActivity(Activity.java:2933)
03-23 12:02:45.249: E/AndroidRuntime(333): at com.example.tasksfull.UndergraduateListActivity.onClick(UndergraduateListActivity.java:61)
03-23 12:02:45.249: E/AndroidRuntime(333): at android.view.View.performClick(View.java:2485)
03-23 12:02:45.249: E/AndroidRuntime(333): at android.view.View$PerformClick.run(View.java:9080)
03-23 12:02:45.249: E/AndroidRuntime(333): at android.os.Handler.handleCallback(Handler.java:587)
03-23 12:02:45.249: E/AndroidRuntime(333): at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 12:02:45.249: E/AndroidRuntime(333): at android.os.Looper.loop(Looper.java:123)
03-23 12:02:45.249: E/AndroidRuntime(333): at android.app.ActivityThread.main(ActivityThread.java:3647)
03-23 12:02:45.249: E/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 12:02:45.249: E/AndroidRuntime(333): at java.lang.reflect.Method.invoke(Method.java:507)
03-23 12:02:45.249: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-23 12:02:45.249: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-23 12:02:45.249: E/AndroidRuntime(333): at dalvik.system.NativeStart.main(Native Method)
03-23 12:02:49.739: I/Process(333): Sending signal. PID: 333 SIG: 9