Unable to get phone number from contact list... code inside
11:08 23 Jul 2011

I'm new to android and programming in general (I'm a linux sys admin / scripting person). And I've been trying to get a simple android app going to get a feel for programming but I am having some problems.

I was able to string together some ideas and queries to get a list view of all the contacts, but now id like to be able to grab the phone number of the selected contact but I am not having any luck... I know there are probably better ways to do what I am doing but I'm just trying to get a grasp on this.

Code first:

package com.SomeApp;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.database.Cursor;
//import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class CallAppActivity extends ListActivity 
{  /** Called when the activity is first created. */
    private ListAdapter mAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        //Get All information from ContactsContract
        Cursor managedCursor = getContentResolver().query(
          ContactsContract.Contacts.CONTENT_URI, 
          null, 
          null, 
          null, 
          null
        );
            
        //Activity will manage the cursor lifecycle.
        startManagingCursor(managedCursor);
        String[] columns = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
        int[] names = new int[] {R.id.contact_entry};
        mAdapter = new SimpleCursorAdapter(
          this, 
          R.layout.main, 
          managedCursor, 
          columns, 
          names
       );
       setListAdapter(mAdapter);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
        super.onListItemClick(l, v, position, id);
    
        Cursor lookup = (Cursor) mAdapter.getItem(position); 
        String contactId = lookup.getString(
          lookup.getColumnIndex(
            ContactsContract.Contacts._ID
          )
        );
    
        Cursor phone_num = getContentResolver().query(
          ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
          null, 
          ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
          null, 
          null
        );
    
        String phoneId = phone_num.getString(
          phone_num.getColumnIndex(
            ContactsContract.CommonDataKinds.Phone.NUMBER
          )
        );
 
        //We are going to print shit to a message box....
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        alertbox.setMessage(phoneId);
        alertbox.show();
    }
}

And now the logcat....

07-23 11:35:34.469: WARN/dalvikvm(781): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-23 11:35:34.479: ERROR/AndroidRuntime(781): FATAL EXCEPTION: main
07-23 11:35:34.479: ERROR/AndroidRuntime(781): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

No matter what Contact I pick I get the same exception.

java android