Q:

Not able to access phone number column through contact api (ContactsContract)

I am trying to assign an contact cursor to an adapter. I am not able to access it.
It throws below error.
Unable to start activity java.lang.IllegalArgumentException: column 'data1' does not exist
in line
*dataAdapter = new SimpleCursorAdapter(this, R.layout.contact_xml, cCursor, columns, to);*
Below is the list view of contacts is what i am trying to achieve through adapter.
---------------------------------------
Contact_name_1         
Phone_no
---------------------------------------
Contact_name_2         
Phone_no2
---------------------------------------
Contact_name_3         
Phone_no3
---------------------------------------

Code below.
public class NewContactTest1 extends Activity{

    ArrayAdapter<String> adapter;
    private SimpleCursorAdapter dataAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    Cursor cCursor = getAllCont();

    String[] columns = new String[]{

                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER
               };

        int[] to = new int[] { 
                R.id.name,
                R.id.phnumber
        };

    dataAdapter = new SimpleCursorAdapter(this, R.layout.contact_xml, cCursor, columns, to);   // I am getting error in here. 

    ListView listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(dataAdapter);
    }

    public Cursor getAllCont()
    {
        Cursor cursor =
                getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                        null, null, null, null);
         if (cursor != null) 
         {  
             cursor.moveToFirst();
         }
         return cursor;
    }

It throws below error.
contact_xml.xml
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFF"
    android:orientation="horizontal"
    android:padding="5dip"
>

    <TextView 
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="id"
        android:visibility="invisible"
    />
    <TextView 
        android:id="@+id/phnumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sent_to"
        android:textColor="#333333"
        android:typeface="serif"
        android:textSize="15dip"
        android:background="#FFF"
    />

</RelativeLayout>

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

  <ListView 
    android:id="@+id/list_view"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 

  ></ListView>

</LinearLayout>

A:

Accessing Contacts can be a bit tricky, but luckily this has been solved previously on StackOverflow:

Android contacts Display Name and Phone Number(s) in single database query?

The top answer appears to answer your question.

