如何从联系人列表中获取特定选定联系人的所有数据

时间:2019-03-02 09:46:28

标签: android contacts

我要实现的是从选定联系人获取给定名称,姓氏,中间名,图像,phonenumber1,phonenumber2数据。

我尝试了该站点的许多解决方案,但出现错误,我认为可能是由于query。 执行代码时出现错误:

打开联系人:

 Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult(intent , REQUEST_CODE_ADDRESS_BOOK);

选择联系人:

   @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);
        switch (reqCode) {
            case (REQUEST_CODE_ADDRESS_BOOK):
                if (resultCode == Activity.RESULT_OK) {

                    Uri resultUri = data.getData();
                    Cursor cont = getContentResolver().query(resultUri, null, null, null, null);
                    if (!cont.moveToNext()) {
                        Toast.makeText(this, "Cursor contains no data", Toast.LENGTH_LONG).show();
                        return;
                    }
                    int columnIndexForId = cont.getColumnIndex(ContactsContract.Contacts._ID);
                    String contactId = cont.getString(columnIndexForId);


                    String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?";
                    String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, contactId };
                    Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
                    nameCur.moveToFirst();
                        String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                        String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                        String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));

                        ViewDataDialog(given,family);
                    nameCur.close();
                }
                break;
        }
    }

Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.quad14.democontact, PID: 2348
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/data/39 flg=0x1 }} to activity {com.quad14.democontact/com.quad14.democontact.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3607)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3650)
        at android.app.ActivityThread.access$1400(ActivityThread.java:154)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
     Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
        at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
        at android.database.CursorWrapper.getString(CursorWrapper.java:114)
        at com.quad14.democontact.MainActivity.onActivityResult(MainActivity.java:85)
        at android.app.Activity.dispatchActivityResult(Activity.java:6192)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3603)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3650) 
        at android.app.ActivityThread.access$1400(ActivityThread.java:154) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5292) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
I/Process: Sending signal. PID: 2348 SIG: 9
Application terminated.

0 个答案:

没有答案