androidandroid-contactsandroid-cursor

Invalid column Contact_ID ContactsContract.Contacts


I am trying the following code to AutoComplete contact details as the user types for my android app.

public class MakePayment extends Activity {
    private AutoCompleteTextView mAuto;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.makepayment);

        mAuto = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewTest);
        ContentResolver content = getContentResolver();
        Cursor cursor = content.query(ContactsContract.Contacts.CONTENT_URI,
                PEOPLE_PROJECTION, null, null, null);

        ContactListAdapter adapter = new ContactListAdapter(this, cursor);
        mAuto.setAdapter(adapter);
    }

     public static class ContactListAdapter extends CursorAdapter implements Filterable {
            public ContactListAdapter(Context context, Cursor c) {
                super(context, c);
                mContent = context.getContentResolver();
            }

            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                 final LayoutInflater inflater = LayoutInflater.from(context);
                    final TextView view = (TextView) inflater.inflate(
                            android.R.layout.simple_dropdown_item_1line, parent, false);
                    view.setText(cursor.getString(3));
                    return view;
            }        

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                ((TextView) view).setText(cursor.getString(3));

            }

            @Override
            public String convertToString(Cursor cursor) {
                return cursor.getString(3);
            }

            @Override
            public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
                if (getFilterQueryProvider() != null) {
                    return getFilterQueryProvider().runQuery(constraint);
                }

                StringBuilder buffer = null;
                String[] args = null;
                if (constraint != null) {
                    buffer = new StringBuilder();
                    buffer.append("UPPER(");
                    buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
                    buffer.append(") GLOB ?");
                    args = new String[] { constraint.toString().toUpperCase() + "*" };
                }

                return mContent.query(ContactsContract.Contacts.CONTENT_URI, PEOPLE_PROJECTION,
                        buffer == null ? null : buffer.toString(), args,
                        null);
            }

            private ContentResolver mContent;           
        }

     private static final String[] PEOPLE_PROJECTION = new String[] {
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Email.DATA,
            ContactsContract.Contacts.DISPLAY_NAME,
        };

}

But my app shuts down giving the following errors:

06-21 10:33:26.970: E/AndroidRuntime(31086): FATAL EXCEPTION: main
06-21 10:33:26.970: E/AndroidRuntime(31086): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.MyApp/com.MyApp.MakePayment}: java.lang.IllegalArgumentException: Invalid column contact_id
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1713)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1541)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:696)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.widget.TabHost.setCurrentTab(TabHost.java:328)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:134)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:518)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.view.View.performClick(View.java:2485)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.view.View$PerformClick.run(View.java:9089)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.os.Handler.handleCallback(Handler.java:587)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.os.Looper.loop(Looper.java:130)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.app.ActivityThread.main(ActivityThread.java:3906)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at java.lang.reflect.Method.invokeNative(Native Method)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at java.lang.reflect.Method.invoke(Method.java:507)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at dalvik.system.NativeStart.main(Native Method)
06-21 10:33:26.970: E/AndroidRuntime(31086): Caused by: java.lang.IllegalArgumentException: Invalid column contact_id
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:372)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:408)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.content.ContentResolver.query(ContentResolver.java:266)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at com.MyApp.MakePayment.onCreate(MakePayment.java:28)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-21 10:33:26.970: E/AndroidRuntime(31086):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1677)

It says invalid column contacts_id. Am I doing anything wrong here?


Solution

  • use

     Cursor cursor = content.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    PEOPLE_PROJECTION, null, null, null);
    

    instead of

     Cursor cursor = content.query(ContactsContract.Contacts.CONTENT_URI,
                    PEOPLE_PROJECTION, null, null, null);
    

    and see docs for ContactsContract.Contacts CONTACT_ID,NUMBER,DATA and DISPLAY_NAME is not filed of ContactsContract.Contacts these all are in ContactsContract.CommonDataKinds