I have a code that picks contacts, the problem is that if a user pick a contact or choose a contact that was not saved on a phone but was saved on google contacts (This is a way many people now save their contacts so that should their phones are lost but contacts are ever lost because when s/he gets a new phone, s/he will just sign in with google account and get his/her contacts) now my app is able to get the name and return it to an EditText but the number/phone number is not returned in these contacts. Anyone knows what the solution to this? Here is my code:

       switch(requestCode){
            case (1):
                if (resultCode == Activity.RESULT_OK){
                    Uri contactData = data.getData();
                    Cursor c = getActivity().managedQuery(contactData, null,null,null,null);
                    if (c.moveToFirst()){
                        String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                        //String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        //txtNumber.setText(number);
                        txtName.setText(name);
                    }
                    Cursor cursor = null;
                    String phoneNumber = "";
                    List<String> allNumbers = new ArrayList<String>();
                    int phoneIdx = 0;
                    try{
                        Uri xresult = data.getData();
                        String id = xresult.getLastPathSegment();
                        cursor = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] {id},null);
                        phoneIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                        if(cursor.moveToFirst()){
                            while (cursor.isAfterLast() == false){
                                phoneNumber = cursor.getString(phoneIdx);
                                allNumbers.add(phoneNumber);
                                txtNumber.setText(phoneNumber);
                                cursor.moveToNext();
                            }
                        }else{
                            // No result actions
                        }
                    } catch (Exception e){
                        // Error actions
                    } finally {
                        if(cursor !=null){
                            cursor.close();
                        }

                        final CharSequence[] items = allNumbers.toArray(new String[allNumbers.size()]);
                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                        builder.setTitle("Choose a number");
                        builder.setItems(items,new DialogInterface.OnClickListener(){
                            public void onClick(DialogInterface dialog,int item){
                                String selectedNumber = items[item].toString();
                                selectedNumber = selectedNumber.replace("-","");
                                txtNumber.setText(selectedNumber);
                            }
                        });
                        AlertDialog alert = builder.create();
                        if(allNumbers.size()>1){
                            alert.show();
                        }else {
                            String selectedNumber = phoneNumber.toString();
                            selectedNumber = selectedNumber.replace("-","");
                            txtNumber.setText(selectedNumber);
                        }
                        if(phoneNumber.length() == 0){
                            Toast.makeText(getActivity(),"Sorry no number here.",Toast.LENGTH_LONG).show();
                        }
                    }
                }
        }

If I select these kind of contacts I get Toast.makeText(getActivity(),"Sorry no number here.",Toast.LENGTH_LONG).show(); message.

Recommended Answers

All 6 Replies

Regarding the permissions, contacts that are saved directly to a phone or sim card can be read both name and number, but these contacts that are saved o google only a name is read but the number is not read.

Sounds like a limitation to me. Is there any example that shows a read from contacts in the cloud?

WhatsApp does read because these othe 6 phones which contacts are stored on google contacts they can use WhatsApp as if contacts are stored in the phone, you wont notice till you go down in developing a contact picker which is when I also knew about this or notice it.

Now you need to find a source code example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.