Fetch Contacts in android application

I was following these links to get the contacts in my application

How to call Android contacts list?

http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

I can display the list of contacts on phone but

-

  • I want to add a checkbox at each contact so that user can select multiple contacts and by clicking the done button he should be able to get all the contacts he selected

  • Also I want to get the name of contact as well as the phone number of contact , see my code :

  •     if (resultCode == Activity.RESULT_OK) {
                        Uri contactData = data.getData();
                        Cursor c = managedQuery(contactData, null, null, null, null);
                        if (c.moveToFirst()) {
                            String name = c.getString(c
                                    .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                            String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                            Log.v("name", name +" "+number);
                            // TODO Whatever you want to do with the selected contact
                            // name.
                        }
                    }
    

    on log cat it shows output as :

    01-09 12:46:41.688: V/name(699): Xyz 1
    

    that is the name of contact is xyz and it has atleast 1 phone number associated with it.Please help me on how can i get the number associated with that contact.

    EDIT :

    if i use this( String number = c.getString(c.getColumnIndexOrThrow(People.NUMBER)); ) line in code the I get following exception :

    01-09 13:33:23.008: E/AndroidRuntime(786): FATAL EXCEPTION: main
    01-09 13:33:23.008: E/AndroidRuntime(786): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-2C2E30/1 (has extras) }} to activity {sra.com/sra.com.ContactsDemo}: java.lang.IllegalArgumentException: column 'number' does not exist
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.ActivityThread.access$2800(ActivityThread.java:125)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.os.Handler.dispatchMessage(Handler.java:99)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.os.Looper.loop(Looper.java:123)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.ActivityThread.main(ActivityThread.java:4627)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at java.lang.reflect.Method.invokeNative(Native Method)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at java.lang.reflect.Method.invoke(Method.java:521)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at dalvik.system.NativeStart.main(Native Method)
    01-09 13:33:23.008: E/AndroidRuntime(786): Caused by: java.lang.IllegalArgumentException: column 'number' does not exist
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at sra.com.ContactsDemo.onActivityResult(ContactsDemo.java:49)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.Activity.dispatchActivityResult(Activity.java:3890)
    01-09 13:33:23.008: E/AndroidRuntime(786):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
    01-09 13:33:23.008: E/AndroidRuntime(786):  ... 11 more
    

    By seeing the answers, I think you got the answer how to fetch contacts and now you want to get the selected contacts on your activity.

    To fetch contact number specific to the contact name:

    ContentResolver contactResolver = getContentResolver(); 
    Cursor cursor = contactResolver.query(Phone.CONTENT_URI, null, Phone.DISPLAY_NAME + "=?", new String[]{contactName}, null);
    
    if(cursor.getCount() > 0){
        cursor.moveToFirst();
        do {
           String number = cursor.getString(mCursor.getColumnIndex(Phone.NUMBER));
        }while (cursor.moveToNext() ); 
    }
    

    Note: Here contactName is the contact name of which you want to fecth contact numbers.

    I am assuming that you have shown the contacts with checkbox in ListView and here is your solution to get the contact list selected by the user to your activity:

    1. Start your contact activity with startActivityForResult() .

    2. Initialize ArrayList variable in contact activity say it contactArrayList .

    3. When user checks the checkbox , add this contact in your contactArrayList and keep on adding and when unchecks then remove the contact from the contactArrayList .

    4. When user presses done then set the result ok with the selected contact list which you have added in contactArrayList like this:

    Intent intent = new Intent();
    Bundle bundle = new Bundle();
    bundle.putStringArrayList("contacts", contactArrayList);
    intent.putExtras(bundle);
    setResult(RESULT_OK, intent);
    

    and finish() this activity.

    5. On your calling activity override:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if(resultCode == RESULT_OK && data != null ){
                 Bundle bundle = new Bundle();
                 bundle =  data.getExtras();
                 ArrayList list = bundle.getStringArrayList("contacts");
        }
    }
    

    Note: The above code was tested over 2.3.3.


    请试试这个代码

    Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        String name, number = "";
        String id;
        c.moveToFirst();
        for (int i = 0; i < c.getCount(); i++) {
            name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
    
            if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id },
                        null);
                while (pCur.moveToNext()) {
                    number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
            }
            Log.i("name ", name + " ");
            Log.i("number ", number + " ");
            c.moveToNext();
    

    In here you have got the cursor c

    Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null,null, null);
    

    to get the name

    String name = c.getString(c.getColumnIndex(People.NAME));
    

    to get the number

    String number = c.getString(c.getColumnIndex(People.NUMBER));
    

    You can iterate through the cursor and save them to a list. Create a checkbox list view and bind with that list.

    链接地址: http://www.djcxy.com/p/31982.html

    上一篇: 使用部分数据查询联系人

    下一篇: 在android应用程序中获取联系人