How to create Android Contact Book App : Part 4 : Display Contact Data from SQLite Database

In this part we are going to cover how to create custom adapter for ListView and display data on ListView.

Custom ListView

Custom ListView

I recommend, before this part start just go through previous 3 parts and take a quick look, so you will get continues reference.
PART 1 : SETTING UP INTERFACE
PART 2 : CREATING SQLITE DATABASE
PART 3 : ADDING CONTACT DATA TO SQLITE DATABASE

So let’s start…..


Table of Contents

  1. Main Layout and Custom ListView Item Layout
  2. Creating Custom Adapter for ListView
  3. Display Data from SQLite Database
  4. Pushing contacts to Test
  5. Download Source

Main Layout and Custom ListView Item Layout

As we have seen in the First Part, How we have created layouts for ListView and ListView Item. Just take a look before start.

And Layout for Custom ListView Item. See Part 1: Setting up Interface

Custom ListView Item

Custom ListView Item

Creating Custom Adapter for ListView

Now we are going to implement a custom adapter class. In this class we will reference custom Layout for ListView Item and set properties of widget controls and then return it.
Right click on src>New>Class. Set super class as “android.widget.BaseAdapter” and name it “CustomAdapter”. See the complete class.

Display Data from SQLite Database

Before fetching contact data let’s see how we have created SQLite databse in Part 3.

In Main.java initiate Custom Adapter and set it to ListView.

Pushing contacts to Test

Now add few contacts to contact book by Click on Add new contact button on Main Activity. So we can test the Custom ListView.

Add Contact

Add Contact

6 replies
    • Ajay Bagujar
      Ajay Bagujar says:

      Please see the function below

      public Contact getContact(int id) {
      SQLiteDatabase db = this.getReadableDatabase();

      Cursor cursor = db.query(TABLE_CONTACTS, new String[] { COLUMN_ID,
      COLUMN_NAME, COLUMN_PHONE }, COLUMN_ID + "=?",
      new String[] { String.valueOf(id) }, null, null, null, null);
      if (cursor != null)
      cursor.moveToFirst();

      Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
      cursor.getString(1), cursor.getString(2));
      // return contact
      return contact;
      }

      Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *