Extracting Contacts from Phone

Purpose: I want to display phone contacts into a ListView.

If you have not yet done the ListView tutorial, you may want to visit the tutorial first to understand the concept of ListView. Otherwise, we will begin our tutorial on how to extract the contact information from your phone book.

The main purpose is to extract the contact (name, in this example) and display on the UI for multiple selections. The result will be then written to the textview.

Before we starts, we need to populate some contacts in our emulator. If your contact list is empty, you should start populate some contacts.



Step 1: Android Permission

First of all, we need to modify the androidmanifest.xml file. This file plays important roles whenever the application needs extra permissions such as accessing to GPS, contact list or internet. Add the uses-permission to allow the "android.permission.READ_CONTACTS" permission.



  
  

  
      
          
              
              
              
      
  
  



Step 2: Build Layout

Now, we will use the same layout as previous tutorial in ListView.










Step 3: Program

We have now given the application for the permission to read the contacts from phone book in step 1. We have built our layout in step 2 so that our result can be shown on the UI. Lastly, we now going to write our codes to read the contact from phone book.
package edu.mobilestudent.readContactFromPhone;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ReadContactFromPhoneActivity extends Activity {
 TextView selection;
 ListView iView;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        
        //namesList is to hold all the extracted names from phone book
        ArrayList namesList = new ArrayList();
        ContentResolver cr = getContentResolver(); //to communicate to phone book
        
        //Cursor is used to locate the database
        Cursor pc= cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        pc.moveToFirst();
        while (pc.isAfterLast()==false){
         String name=pc.getString(pc.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
         namesList.add(name);
         pc.moveToNext();
        }
        
        //Copy the arrayList into Array
        final String[] mString=(String[])namesList.toArray(new String[namesList.size()]);

        //Communicate to layout by definition
        iView=(ListView)findViewById(R.id.list);
        selection=(TextView)findViewById(R.id.selection);

        //Copy the array into ListView with option - multiple selections
        iView.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice,mString));
        iView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        
        //Event triggered upon selection
        iView.setOnItemClickListener(new OnItemClickListener(){
         @Override
         public void onItemClick(AdapterView parent, View v, int pos, long id) {
          selection.setText("");
          int len = iView.getCount();
          SparseBooleanArray checked = iView.getCheckedItemPositions();
          for (int i=0; i<len; i++){
           if (checked.valueAt(i)) selection.append(mString[checked.keyAt(i)]+" ");
          }    
         }
        });
    }
}