3

I managed to write add program that upon the user click a button, he will be able to retrieve a phone number that will fill an editText box. The problem is that if a contact has multiple numbers, it will always take the top most number.

I have been reading another thread, Getting Number from Contacts Picker, there is an answer, but I don't get it. As I am new to android programming, I'll appreciate if anyone out there could give me step by step instructions.

Community
  • 1
  • 1
Akiff
  • 69
  • 1
  • 9

1 Answers1

2

First, you may need to query all contact in phone book.

        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME
        };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        // Build adapter with contact entries
        Cursor mCursor = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
        //
        // Bind mCursor to to your Listview
        //

After that, when user select a contact in your list view, you make a second query to get label and number of that contact.

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        mCursor.moveToPosition(position);
        startManagingCursor(mCursor);       
        String contactID = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));

        Cursor phoneNumCursor = getContentResolver().query(Phone.CONTENT_URI,  
                null, Phone.CONTACT_ID + "=?", new String[] { contactID }, null); 

        phoneNumCursor.moveToFirst();

        Vector<String> phoneTypeList = new Vector<String>();
        Vector<String> phoneNumberList = new Vector<String>();

        while (!phoneNumCursor.isAfterLast()){
            int type = phoneNumCursor.getInt(phoneNumCursor.getColumnIndex(Phone.TYPE));
            phoneTypeList.add(String.valueOf(Phone.getTypeLabel(getResources(),type,"")));
            phoneNumberList.add(phoneNumCursor.getString(phoneNumCursor.getColumnIndex(Phone.NUMBER)));
            phoneNumCursor.moveToNext();
        }
        //
        // Feel free to show label and phone number of that contact. ^^
        //

Updated:

Below is an example if you want to use Contact Picker.

    private static final int CONTACT_PICKER_RESULT = 1001;

    protected void startContactPicker(){
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);  
        startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT); 
    }

    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (resultCode == RESULT_OK) {  
            switch (requestCode) {  
            case CONTACT_PICKER_RESULT:  
                Cursor cursor = null;  
                String phoneNumber = "";  
                try {  
                    Uri result = data.getData();
                    String id = result.getLastPathSegment();
                    cursor = getContentResolver().query(Phone.CONTENT_URI,  
                            null, Phone.CONTACT_ID + "=?", new String[] { id }, null);  

                    int phoneIdx = cursor.getColumnIndex(Phone.DATA);  
                    if (cursor.moveToFirst()) {  
                        while (!cursor.isAfterLast()){
                            phoneNumber = cursor.getString(phoneIdx);
                            //
                            // this will go through all phoneNumber of selected contact.
                            //

                            cursor.moveToNext();
                        }   
                    }  
                } catch (Exception e) {
                } finally {  
                    if (cursor != null) {  
                        cursor.close();  
                    } 
                    numberView.setText(phoneNumber);

                }  

                break;  
            }        
        }
    }
PhatHV
  • 8,010
  • 6
  • 31
  • 40
  • list view meaning , i have to add in XML? – Akiff Mar 21 '12 at 06:11
  • You can put it in XML or do it by java code to show list of all contact name. – PhatHV Mar 21 '12 at 06:39
  • thanks for answering though! i try to go thru slowly and understand the program. Im currently lost – Akiff Mar 21 '12 at 07:33
  • If you want to use Contact Picker. just forget first code block. In method `onActivityResult` you only need this: `String contactID = result.getLastPathSegment();` for getting ID of selected contact. and then query to get type labels and phone numbers of that contact. – PhatHV Mar 21 '12 at 08:24
  • Pls, see my updated answer and feel free to take a try with this. – PhatHV Mar 21 '12 at 08:32
  • i managed to do it. The only problem is how do i display the numbers and number type together? – Akiff Mar 22 '12 at 03:12
  • put code below near `phoneIdx` line and before while loop of retrieving all phone number. `typeIdx=cursor.getColumnIndex(Phone.TYPE);` and put codes below in while loop to get number type. `int type = cursor.getInt(typeIdx); String.valueOf(Phone.getTypeLabel(getResources(),type,""));` Hope this help. – PhatHV Mar 22 '12 at 03:40
  • Thank You Very Much Huynh Vinh Phat! :D – Akiff Mar 22 '12 at 04:47
  • @HyunhVinhPhat is there any way to just retrieve favourite contact using the same method :) ? – Akiff Jul 23 '12 at 05:55
  • hi Akiff, you can try `cursor = getContentResolver().query(Phone.CONTENT_URI, null, "starred=?",new String[] {"1"}, null);`. – PhatHV Jul 25 '12 at 08:36
  • @HyunhVinhPhat your suggested method, displays all the contact. upon clicking, it returns all the phone number that is associated with all the favorite contact. Meaning if i have 2 favourite contact, All the numbers of the 2 users will be displayed in a list. – Akiff Jul 26 '12 at 04:15
  • @Akiff: you may try `cursor = getContentResolver().query(Phone.CONTENT_URI, null, "starred=? AND " + Phone.CONTACT_ID + "=?",new String[] {"1",id}, null);`. – PhatHV Jul 26 '12 at 06:40
  • @HyunhVinhPhat this works fine! I see all the contacts in the book and those favorite with a star. Only those favorite will return number/numbers. Thank you so much! Just wondering can i modify the cursor to get numbers from the call log? – Akiff Jul 26 '12 at 12:50
  • @HyunhVinhPhat thanks! the command in cursor, is for SQL database right? – Akiff Jul 28 '12 at 06:22
  • @Akiff. Cursor is cursor. You can use it to point to your container. Try to google "Cursor in Android". ^^ – PhatHV Jul 30 '12 at 02:50
  • Thank you so much! You really help me! And sorry if i spelt your name wrongly :) – Akiff Jul 31 '12 at 01:57