Fetch Contacts From Android Phone

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. 
 
Now we will the contactbean object as an argument to the add() method to add it to a list. We will this list to the ListView . 
 
Step 1
 
Create an XML file and write this: 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.    
  7.     <ListView  
  8.         android:id="@+id/listviewshow"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.       android:background="#C0C0C0" />  
  12. </RelativeLayout>  
Step 2
 
Create another XML file and write this:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/relativelay"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:background="@drawable/bg_row"  
  7.     android:orientation="vertical" >  
  8.    
  9.     <TextView  
  10.         android:id="@+id/tvname"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_marginLeft="3dp"  
  14.         android:layout_marginTop="3dp"  
  15.         android:text="Title"  
  16.         android:textSize="17dp"  
  17.         android:textStyle="bold"  
  18.             android:textColor="#2F1F1F">  
  19.     </TextView>  
  20.    
  21.     <TextView  
  22.         android:id="@+id/tvphone"  
  23.         android:layout_width="fill_parent"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_below="@+id/tvname"  
  26.         android:layout_marginLeft="3dp"  
  27.         android:layout_marginTop="10dp"  
  28.         android:text="Phone no"  
  29.         android:textSize="14dp"  
  30.         android:textColor="#2F1F1F">  
  31.     </TextView>  
  32.    
  33.    
  34. </RelativeLayout> 
Create an XML file and write this:
[The XML is missing]
 
Step 3
 
Create a Java ContactlistActivity class and write this:
  1. package com.samir;  
  2. import android.app.Activity;  
  3. import android.app.AlertDialog;  
  4. import android.content.DialogInterface;  
  5. import android.database.Cursor;  
  6. import android.os.Bundle;  
  7. import android.provider.ContactsContract;  
  8. import android.view.View;  
  9. import android.widget.AdapterView;  
  10. import android.widget.AdapterView.OnItemClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.ListView;  
  13. import android.widget.Toast;  
  14. import java.util.ArrayList;  
  15. import java.util.Collections;  
  16. import java.util.Comparator;  
  17. import java.util.List;  
  18. public class ContactListActivity extends Activity implements  
  19. OnItemClickListener {  
  20.  Button button;  
  21.  private ListView mylistView;  
  22.  private List < ContactBean > mylist = new ArrayList < ContactBean > ();  
  23.  @Override  
  24.  protected void onCreate(Bundle savedInstanceState) {  
  25.   super.onCreate(savedInstanceState);  
  26.   setContentView(R.layout.main);  
  27.   mylistView = (ListView) findViewById(R.id.listviewshow);  
  28.   mylistView.setOnItemClickListener(this);  
  29.   Cursor phonescursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, nullnullnullnull);  
  30.   while (phonescursor.moveToNext()) {  
  31.    String name = phonescursor.getString(phonescursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));  
  32.    String phoneNumber = phonescursor.getString(phonescursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
  33.    ContactBean objContact = new ContactBean();  
  34.    objContact.Nameset(name);  
  35.    objContact.PhoneNoset(phoneNumber);  
  36.    mylist.add(objContact);  
  37.   }  
  38.   phonescursor.close();  
  39.   ContanctAdapter objAdapter = new ContanctAdapter(ContactListActivity.this, R.layout.alluser_row, mylist);  
  40.   mylistView.setAdapter(objAdapter);  
  41.   if (null != mylist && mylist.size() != 0) {  
  42.    Collections.sort(mylist, new Comparator < ContactBean > () {  
  43.     @Override  
  44.     public int compare(ContactBean lhs, ContactBean rhs) {  
  45.      return lhs.Nameget().compareTo(rhs.Nameget());  
  46.     }  
  47.    });  
  48.    AlertDialog alert = new AlertDialog.Builder(ContactListActivity.this).create();  
  49.    alert.setTitle("");  
  50.    alert.setMessage(mylist.size() + " Contact Found!!!");  
  51.    alert.setButton("OK"new DialogInterface.OnClickListener() {  
  52.     @Override  
  53.     public void onClick(DialogInterface dialog, int which) {  
  54.      dialog.dismiss();  
  55.     }  
  56.    });  
  57.    alert.show();  
  58.   } else {  
  59.    showToast("No Contact Found!!!");  
  60.   }  
  61.  }  
  62.  private void showToast(String msg) {  
  63.   Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();  
  64.  }  
  65.  @Override  
  66.  public void onItemClick(AdapterView < ? > listview, View v, int position, long id) {  
  67.   ContactBean bean = (ContactBean) listview.getItemAtPosition(position);  
  68.  }  

Step 4
 
Create another Java class file and write this:
  1. package com.samir;  
  2. public class ContactBean {  
  3.  private String name;  
  4.  private String phoneNo;  
  5.   
  6.  public String Nameget() {  
  7.   return name;  
  8.  }  
  9.  public void Nameset(String name) {  
  10.   this.name = name;  
  11.  }  
  12.  public String PhoneNoget() {  
  13.   return phoneNo;  
  14.  }  
  15.  public void PhoneNoset(String phoneNo) {  
  16.   this.phoneNo = phoneNo;  
  17.  }  
Step 5
 
Create another Java class file and write this:
  1. package com.samir;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.text.Html;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.ArrayAdapter;  
  9. import android.widget.TextView;  
  10. import java.util.List;  
  11. public class ContanctAdapter extends ArrayAdapter < ContactBean >   
  12. {  
  13.  private Activity activity;private List < ContactBean > contacts;private int row;private ContactBean objBean;public ContanctAdapter(Activity act, int row, List < ContactBean > items)   
  14.  {  
  15.   super(act, row, items);  
  16.   this.activity = act;  
  17.   this.row = row;  
  18.   this.contacts = items;  
  19.  }  
  20.  @Override  
  21.  public View getView(final int position, View convertView, ViewGroup parent)   
  22.  {  
  23.   View view = convertView;  
  24.   ViewHolder holder;  
  25.   if (view == null)   
  26.   {  
  27.    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  28.    view = inflater.inflate(row, null);  
  29.    holder = new ViewHolder();  
  30.    view.setTag(holder);  
  31.   }   
  32.   else   
  33.   {  
  34.    holder = (ViewHolder) view.getTag();  
  35.   }  
  36.   if ((contacts == null) || ((position + 1) > contacts.size()))  
  37.    return view;  
  38.   objBean = contacts.get(position);  
  39.   holder.name = (TextView) view.findViewById(R.id.tvname);  
  40.   holder.PhoneNo = (TextView) view.findViewById(R.id.tvphone);  
  41.   if (holder.name != null && null != objBean.Nameget() && objBean.Nameget().trim().length() > 0)   
  42.   {  
  43.    holder.name.setText(Html.fromHtml(objBean.Nameget()));  
  44.   }  
  45.   if (holder.PhoneNo != null && null != objBean.PhoneNoget() && objBean.PhoneNoget().trim().length() > 0)   
  46.   {  
  47.    holder.PhoneNo.setText(Html.fromHtml(objBean.PhoneNoget()));  
  48.   }  
  49.   return view;  
  50.  }  
  51.  public class ViewHolder   
  52.  {  
  53.   public TextView name, PhoneNo;  
  54.  }  

Step 6
 
Output:
 
Clipboard08.jpg
 
Step 7
 
After clicking on the "Ok" button: 
 
Clipboard06.jpg


Similar Articles