Getting second number from contacts in Android

I am trying to get the second number from a contact. I can get the first number right now. I would like to receive a second or third number on a specific contact. Is it possible?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if(resultCode == RESULT_OK){
        if(requestCode == RQS_PICKCONTACT){
            Uri returnUri = data.getData();
            Cursor cursor = getContentResolver().query(returnUri, null, null, null, null);

            if(cursor.moveToNext()){
                int columnIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
                String contactID = cursor.getString(columnIndex_ID);

                int columnIndex_HASPHONENUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
                String stringHasPhoneNumber = cursor.getString(columnIndex_HASPHONENUMBER);

                if(stringHasPhoneNumber.equalsIgnoreCase("1")){
                    Cursor cursorNum = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                            null, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactID, 
                            null, 
                            null);

                    //Get the first phone number
                    if(cursorNum.moveToNext()){
                        int columnIndex_number = cursorNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                        String stringNumber = cursorNum.getString(columnIndex_number);
                        textPhone.setText(stringNumber);
                    }

      

Tell me!

Thank!

+3


source to share


1 answer


Your current code will only give the first phone number, so that all phone numbers just scroll over the cursor, it will give you all the subcategory numbers for a given contact .



while (cursorNum.moveToNext()) {
    int columnIndex_number = cursorNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    String stringNumber = cursorNum.getString(columnIndex_number);
    Log.i(TAG,"Contact number "+stringNumber);

    // do your desired task here. 

}

      

+2


source







All Articles