仅从具有两个以上联系号码的联系人中选择一个号码

时间:2013-02-22 04:44:56

标签: android

我能够使用以下代码从联系人中检索联系人姓名和号码。我的问题是,当联系人有多个号码时,它会以某种方式选择其中一个,这是我不想要的。

如果联系人有多个电话号码,我需要用户能够选择他想要选择的电话号码。

这是我的代码。

private void pickContacts() {
                // TODO Auto-generated method stub
                Intent it = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);           
                startActivityForResult(it,1);
            }

在onActivityResult(int requestCode,int resultCode,Intent data)

if(requestCode==1){
        Uri contactData = data.getData();
        Cursor c = managedQuery(contactData, null, null, null, null);
        if (c.moveToFirst()) 
        {                       
            String id = c.getString(
                    c.getColumnIndex(ContactsContract.Contacts._ID));
             name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            localEditor.putString("contactName", name);
            tv2.setText(name);      
            int hasPhone=c.getInt(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            if(hasPhone==1){
                 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));
                                //  Toast.makeText(getApplicationContext(), number, Toast.LENGTH_LONG).show();

                                    localEditor.putString("contactNumber", number);

                                tv3.setText(number);

                                }
                                pCur.close();
            }
        }while(c.moveToNext());




        localEditor.commit();
      //  tv2.setText(name);     
     //   tv3.setText(number);
    super.onActivityResult(requestCode, resultCode, data);
}

提前感谢您的帮助..

2 个答案:

答案 0 :(得分:1)

Cursor有一个名为getCount的方法,它返回游标中的行数。您可以使用它来查明是否有多个数字并列出它们以选择一个。

试试这个......

if (c.getInt(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);
    if(pCur.getCount() > 1) {
        int i=0;
        String[] phoneNum = new String[pCur.getCount()];
        while (pCur.moveToNext()) {
            // store the numbers in an array
            phoneNum[i] = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            i++;
        }
        // list the phoneNum array (perhaps using radiobuttons) & give the choice to select one number      
    } else {
        // do what you are doing now
        // while (pCur.moveToNext()) {
        //}     
    }
    pCur.close();
}

希望这有帮助。

答案 1 :(得分:0)

fun pickContact() {
                val intent = Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);           
                startActivityForResult(intent, REQUEST_CODE_CONTACT);
            }

使用ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE启动android联系人选择器,它将允许用户为每个联系人拍照单个手机。

相关问题