minesh..
minesh..

Reputation: 155

Selecting only one number from a contact which is having more than two contact numbers

I am able to retrieve contact name and number from contacts using following code.My problem is that when a contact has multiple numbers,it somehow selects one of them,which i don't want.

If a contact has multiple phone numbers, I need the user to be able to choose which phone number he wants to select.

This is my code.

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

In 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);
}

Thank for the help in advance..

Upvotes: 0

Views: 1683

Answers (2)

northerngirl
northerngirl

Reputation: 225

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

Start android contact picker with ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE, it will allow user to pic single phone per contact.

Upvotes: 0

neo108
neo108

Reputation: 5246

Cursor has a method called getCount which returns the number of rows in a cursor. You can use that to find out if there are multiple numbers and list them to choose one.

Try this...

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();
}

Hope this helps.

Upvotes: 1

Related Questions