Sample Code for ListView with SQLite Database Connection in Android

Here We are going to discuss 4 important features in android from development prospective with sample code and example.1. How to use ListView in android with example.

2. How to connect to SQLite database in android with example.
3. How to fetch contacts in android.
4. Finally practical use of how to fetch contacts from Contacts database(SQLite Database)and show in Listview in android.
 There are so many ways to populate the ListView items in android,we can use ArrayAdapter, BaseAdapter etc for this purpose.

In this Post we will discuss how to fetch contacts from contacts database (SQLite Database)and populate ListView items with these contacts.
Here I have created an App which shows the List of Phone Book Contact Names in a listview in Andrid.
Refer the below activity, which contains a Listview and the Listview contains items from phone contacts list. We will explain details in below section.

listview-example-in-android

The above Activity is called PhoneBookActivity in our code. Which shows contacts items in a  listview. Also You will find 2 xml files associated with this application.

1. phone_book.xml       which contains a ListView item
2. each_contact.xml     which contains  a TextView item inside a listview to show the the Contact Names for each item.

Since we reading the contact names so do not forget to include the following permission in the Manifest  file of your project: Add the below line in your Manifest xlm file before the code for lunching your activity.
<uses-permission android:name=”android.permission.READ_CONTACTS”/>

Now coming to the xml layout. Add this 2 xlm files in your project

 XML Layouts:

 1. phone_book.xml 

2. each_contact.xml

 

Now coming to the java file PhoneBookActivity.java. Here you can find the code logic.

downloadDownload Sample Code from Here ListView with SQLite Database Connection in Android.

That’s it. You can  download the code or copy the above code snippet and use it in your application. Kindly post comments if you are facing any issue while using the above said sample code. We will try to resolve your queries asap.

Tips for some common errors while building this android application

You might face some Android key words undefined errors. The problem is you did not add proper packages. So To  add packages automatically, just move your mouse cursor on the error. Eclipse will show you a popup , then add click on the proper package. That’s it.

Note: We have just retrieved contacts from the contacts database, similarly we can retrieve call logs from the call log database. However I will update the example here for how to retrieve call logs from the call log database using using adapter in Android.

I hope this small post on ‘Sample Code for ListView with SQLite Database Connection in Android‘ will help you at it’s best.

1. You might like to learn how to save persistent user data using Android SharedPreference with Example.

2. You might like to learn about Android Login Registration Screen with SQLite Database Example.

 

Post By SmrutiRanjan (54 Posts)

Working @ Samsung as a Project Lead for Android Smartphones. I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon".I am also an active contributor for the blog-"Gadgets n Gizmos World". Job is my necessity, but blogging is my passion.

Website: → Techblogon

Connect

28 Responses to Sample Code for ListView with SQLite Database Connection in Android

  1. Nice work..
    Thanks for Sharing the codes…its helpful…

  2. R Star says:

    Hi!

    Thanks Very much!

  3. Bhavin Soni says:

    Thank you so much for sharing this code , it’s very helpful for me.

  4. Simon C says:

    Thank you for posting this – It is really useful! Just one question – how would I read my own sqlite database instead of the phonebook and where would I save it in my project?

  5. Jalil says:

    hi i would like to have a simple SQLite program with out helper,adapter,cursor just to insert values into the table

    seconldy

    how to clear the error -”close() was never explicitly called on database”
    I was taught database without helper,adapter, and close
    but now in my app it is asking to close i dnt know wht to do pls help me soon

  6. niaa says:

    it’s really helpful. but i want to use the number too.
    how can i get the number?
    please help me..
    thanks…

  7. Prince says:

    First of all thanks a lot. Your code is very clear, easy to understand.
    But when I attempt to run, it throws unfortunately stopped error.

  8. maharshi says:

    hello in this code contact list cant be displayed only black screen loaded so please solve out this problem ane solution will be send to my mail id

    • SmrutiRanjan says:

      Dear Maharshi,
      This code will pick contacts only from the phone not from the SIM card.
      So copy some contacts in phone memory, then try.
      Good luck. If it works just let me know

  9. heel pads shoes too big says:

    Very interesting subject, regards for posting.

  10. rishika says:

    hi, this one is great..

    but i need to import csv file into sqlite database.
    pls help

  11. Amardeep says:

    Hello Sir,
    How to Android connect with External database using java code

    Regards:
    Amardeep

  12. dinh nguyen says:

    Hi,
    I want to ask how to get phone number and name in this example?
    I’m a newbie in Android. Pls help me. Tks so much

  13. candy crush saga cheats says:

    It’s very straightforward to find out any topic on web as compared to books, as I found this paragraph at this site.

  14. Thanks for any other great article. The place else may anyone
    get that type of info in such a perfect approach of writing?
    I’ve a presentation subsequent week, and I’m at the look for such
    info.

  15. jeetu says:

    h sir please plz tell me can i run google map on eclipse emulator no need of device if yes than tell me in detail

  16. José Azevedo says:

    Hello SmrutiRanjan,
    I’ve tried your code and everything works fine.
    My question is: if you have a imageview on the xml file of the list and you want to change the source of the imageview depending on sqlite field value how do you do it?

    Thank you in advance

  17. jay says:

    gud post………….

  18. whatsapp download for pc says:

    You made some good points there. I looked on the internet for more info about the
    issue and found most individuals will go along with your
    views on this website.

  19. George says:

    Hi i finished he login section on sqlite database and i wanna add next activity it’s also belong database how can i add the second database ..

  20. Siddhartha Singh says:

    Hii, Sir I ma very Much Thankful for providing the building blocs of Android,But still I am suffering In UI creation to Correctly placing the ,

    Id’s Handling as well may you help me how to find the SQL-Lite DataBase and to connect it with the Eclipse.

    Please , I am looking for your early response.

  21. candy crush cheats says:

    Magnificent site. Plenty of helpful info here. I’m sending
    it to a few buddies ans also sharing in delicious. And certainly, thanks to your effort!

  22. hyundai used cars says:

    Thanks in support of sharing such a pleasant opinion, piece of writing is good, thats why i have
    read it entirely

  23. site de rencontre says:

    Just wish to say your article is as astounding.
    The clarity on your submit is just excellent and that i could suppose you’re knowledgeable
    on this subject. Well together with your permission let me to take hold of your feed to stay up to date with imminent post.
    Thank you 1,000,000 and please keep up the rewarding work.

Leave a Reply

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


6 − = one

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">