Introduction
This article shows how to fetch contacts from an Android phone and show them in a ListView.
-
Cursor
Cursor is a class that contains data (rows) returned by the query. In this, we used a cursor variable to hold the cursor that is returned by the query. - moveToNext() method
The moveToNext() method iterates the rows of a cursor. - getContentResolver()
The getContentResolver() method gets the object of the contentresolver. - query()
query() is a method provided by the Cursor class that is called with the object of the content resolver to get the Cursor.
In this first, you need to get the cursor object to get the cursor that contains all the information in the form of rows. And to fetch the data from the row you need to set the cursor pointer to the row by the moveToNext() method. Create a variable of the String type to hold the phone and name of the user. After holding it in variables we will create the object of the ContactBean class where we made the four methods Nameget(), Nameset(), PhoneNoget() and PhoneNoset().
The Nameset() method sets the name of when we called it from the object of the ContactBean class.
The PhoneNoset() method sets the phone number when we called it from the object of the contactbean class.
Create an XML file and write this:
Step 2
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <ListView
- android:id="@+id/listviewshow"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#C0C0C0" />
- </RelativeLayout>
Create another XML file and write this:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/relativelay"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/bg_row"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tvname"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="3dp"
- android:layout_marginTop="3dp"
- android:text="Title"
- android:textSize="17dp"
- android:textStyle="bold"
- android:textColor="#2F1F1F">
- </TextView>
- <TextView
- android:id="@+id/tvphone"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_below="@+id/tvname"
- android:layout_marginLeft="3dp"
- android:layout_marginTop="10dp"
- android:text="Phone no"
- android:textSize="14dp"
- android:textColor="#2F1F1F">
- </TextView>
- </RelativeLayout>
Create an XML file and write this:
[The XML is missing]


blu tionPosted Nov 10, 2015, 5:52 AM
Hi I hope you can help. The code above does not compile, it crashes and I noticed the .xml file in step 3 is missing as well. May you please update
Mahesh ChandPosted Jul 9, 2013, 9:29 PM
Do you need to provide user id and password to fetch the contacts?