Android SQLite Database Tutorial II
This is part II of using SQLite in Android tutorials if you haven’t read the first tutorial, please consider doing so. In this part, were going to use the SQLite utility classes Contact
and ContactHelper
that we create previously to create a phone book application were the user can add contacts to the phone book, view contacts in the phone book, view|edit contact details, as well as delete a contact from the phone book.

Project Structure
Add Contact

Android SQLite Add Contact
1 2 3 4 5 6 7 8 9 10 11 12 13 |
private void addContact() { Contact contact = new Contact(); contact.setName(name.getText().toString()); contact.setOrganization(organization.getText().toString()); contact.setPhone(phone.getText().toString()); contact.setAddress(address.getText().toString()); ContactHelper db = new ContactHelper(this); db.addContact(contact); finish(); } |
View Contacts
Now lets view display all the contact in the phone book using a ListView
. We need create a custom CursorAdapter
to load the Contacts cursor that we will receive from the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
private class ContactCursorAdapter extends CursorAdapter { public ContactCursorAdapter(Context context, Cursor cursor, int flags) { super(context, cursor, flags); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return LayoutInflater.from(context).inflate(R.layout.row_contact, parent, false); } @Override public void bindView(View view, Context context, Cursor cursor) { TextView tv_contact_id = (TextView) view.findViewById(R.id.tv_contact_id); TextView tv_contact_name = (TextView) view.findViewById(R.id.tv_contact_name); TextView tv_contact_phone = (TextView) view.findViewById(R.id.tv_contact_phone); int id = cursor.getInt(cursor.getColumnIndexOrThrow(ContactHelper._ID)); String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactHelper.COL_NAME)); String phone = cursor.getString(cursor.getColumnIndexOrThrow(ContactHelper.COL_PHONE)); tv_contact_id.setText(String.valueOf(id)); tv_contact_name.setText(name); tv_contact_phone.setText(phone); } } |
1 2 3 4 5 6 7 |
ContactHelper db = new ContactHelper(this); contacts = db.getContacts(); contactCursorAdapter = new ContactCursorAdapter(this, contacts, 0); lv_contacts.setAdapter(contactCursorAdapter); |
View Contact Details
1 2 3 4 5 6 7 8 9 10 11 |
private void viewContact(int id) { ContactHelper db = new ContactHelper(this); Contact contact = db.getContact(id); name.setText(contact.getName()); organization.setText(contact.getOrganization()); phone.setText(contact.getPhone()); address.setText(contact.getAddress()); } |
Edit Contact
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
private void loadContact(int id) { ContactHelper db = new ContactHelper(this); Contact contact = db.getContact(id); name.setText(contact.getName()); organization.setText(contact.getOrganization()); phone.setText(contact.getPhone()); address.setText(contact.getAddress()); } private View.OnClickListener onClickUpdateBtn = new View.OnClickListener() { @Override public void onClick(View view) { Contact contact = new Contact(); contact.set_id(contact_id); contact.setName(name.getText().toString()); contact.setOrganization(organization.getText().toString()); contact.setPhone(phone.getText().toString()); contact.setAddress(address.getText().toString()); ContactHelper db = new ContactHelper(getApplicationContext()); db.updateContact(contact); finish(); } }; |
Delete Contact
![]() |
![]() |
I have used a Context Menu
to show the Edit and Delete options when a user long presses on a contact in the phone book.
There’s a separate tutorial demonstrating how to implement a Context Menus
for your ListView which you can read here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public AlertDialog.Builder confirmationDelete(final int id, String name) { AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); alertDialog.setCancelable(false); alertDialog.setTitle("Confirmation"); alertDialog.setMessage("You're about to delete '"+name+"'; \nThis is action irreversible"); alertDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int which) { ContactHelper db = new ContactHelper(getApplicationContext()); db.deleteContact(id); contacts = db.getContacts(); contactCursorAdapter.swapCursor(contacts); lv_contacts.setAdapter(contactCursorAdapter); } }); alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); return alertDialog; } |
Thank you for following through this whole tutorial, I have strived to introduce the basic usage of SQLite in Android and hope this tutorial has gotten you started for that purpose. Please feel free to leave me you feedback regarding this tutorail in the comments section and remember; the complete source code for this application is available on GitHub for those that might need it.