androidlistviewandroid-contactscontactscontractandroid-cursoradapter

Getting illegalargumentException: column 'data1' does not exist in Android


I am trying to put Name and phone numbers from contacts to my customized listView with help of Custom CursorAdapter, But I am getting the following error

Exception

 11-07 17:53:39.619: ERROR/AndroidRuntime(628): FATAL EXCEPTION: main
 11-07 17:53:39.619: ERROR/AndroidRuntime(628): java.lang.IllegalArgumentException: column      'data1' does not exist
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at org.com.apis.ContactListCustomCursorAdapter.bindView(ContactListCustomCursorAdapter.java:37)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.widget.CursorAdapter.getView(CursorAdapter.java:186)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.widget.ListView.onMeasure(ListView.java:1109)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.view.View.measure(View.java:8171)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.view.View.measure(View.java:8171)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.view.View.measure(View.java:8171)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.view.View.measure(View.java:8171)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.os.Handler.dispatchMessage(Handler.java:99)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.os.Looper.loop(Looper.java:123)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at java.lang.reflect.Method.invokeNative(Native Method)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at java.lang.reflect.Method.invoke(Method.java:521)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 11-07 17:53:39.619: ERROR/AndroidRuntime(628):     at dalvik.system.NativeStart.main(Native Method)

Here is the Code I am using

Main Activity Code

 public class ManipulationActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.contacts_list_layout);

    Cursor cursor = getCursor();


    ListView contactList = (ListView)findViewById(R.id.contactList);
    contactList.setAdapter(new ContactListCustomCursorAdapter(this, cursor)); //ContactListCustomCursorAdapter is a customized CursorAdapter. I have put the code below this class

}

    //Get Cursor pointing Contacts
private Cursor getCursor() {

    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
    while (cursor.moveToNext()) { 
       String contactId = cursor.getString(cursor.getColumnIndex( 
       ContactsContract.Contacts._ID)); 
       String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
       if (Boolean.parseBoolean(hasPhone)) { 
          // You know it has a number so now query it like this
          Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
          while (phones.moveToNext()) { 
             String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));                 
          } 
       phones.close(); 
       }
    }
    cursor.close();
    return cursor;
}      

}

ContactListCustomCursorAdapter Code

public class ContactListCustomCursorAdapter extends CursorAdapter {

private Context context;
private Cursor cursor;
private LayoutInflater inflater;
private int username;
private int phoneNumber;

public ContactListCustomCursorAdapter(Context context, Cursor c) {
    super(context, c);
    this.context = context;
    this.cursor = c;
    inflater = LayoutInflater.from(context);

    username = cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView userName = (TextView)view.findViewById(R.id.userName);
    userName.setText(cursor.getString(username));

    TextView phoneNumber = (TextView)view.findViewById(R.id.userNumber);
    phoneNumber.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    final View view = inflater.inflate(R.layout.custom_contact_list_item, parent, false);

    return view;
}

}

I don't want to use the Contact Picker.


Solution

  • First of all the cursor you are passing to the CursorAdapter is closed. If you are assigning a cursor to an adapter you should not close it, just call changeCursor(null) in onDestroy for your activity.

    Secondly, from the code I assume you want to show a list of all Contacts and their Phone numbers, so you getCursor method should be something like this

    private Cursor getCursor() {
    
        Cursor cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 
    
        return cursor;
    }
    

    And your bindView should be like

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView userName = (TextView)view.findViewById(R.id.userName);
        userName.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
    
        TextView phoneNumber = (TextView)view.findViewById(R.id.userNumber);
        phoneNumber.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)));
    }