0

Hi i want to get the mobile number and set it in a edittext field.

Button onclick():

btnContacts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, PICK_CONTACT);
            }
        });

onActivityResult() code:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        Cursor cursor = null;
       // mName.setText(context.getString(R.string.not_available));
       // mNumber.setText(context.getString(R.string.not_available));

        if(requestCode == PICK_CONTACT && resultCode == RESULT_OK && data != null){
           // Log.d(TAG, "requestCode, resultCode, data ok");
            Uri uri = data.getData();
            try{
                String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER};
//              cursor =  getContentResolver().query(uri, projection, null, null, null);
                cursor =  getContentResolver().query(uri, null, null, null, null);
                cursor.moveToFirst();
               // Log.d(TAG, "Trying to retrieve the name and the number");
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String hasNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER));

                //Log.d(TAG, "hasNumber "+hasNumber);
                //mName.setText(name);


                 //   Log.d(TAG, "contact has telephone number");
                    //set name and number
                    String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                  //  mNumber.setText(phoneNumber);
                    Toast toast = Toast.makeText(getApplicationContext(),phoneNumber, Toast.LENGTH_SHORT);
                    toast.show();


            }catch(Exception ex){
                Toast toast = Toast.makeText(getApplicationContext(),"fail", Toast.LENGTH_SHORT);
                toast.show();
            }
            if(cursor!= null && !cursor.isClosed()){
                cursor.close();
            }
        }else{

            Toast toast = Toast.makeText(getApplicationContext(),"failed", Toast.LENGTH_SHORT);
            toast.show();
        }
    }

When i click the button the contact book opens, and when i select a contact some error is thrown

Log:

Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 36 columns.

Where am i doing wrong?

The Heist
  • 1,444
  • 1
  • 16
  • 32
diwa
  • 169
  • 2
  • 2
  • 11

1 Answers1

0

ContactsContract has a lot of columns. The column ContactsContract.CommonDataKinds.Phone.NUMBER probably does not contain the mobile number. It may not contain anything...

You need to scroll through all phone numbers entered like this:

How to get contacts' phone number in Android

You could modify the code to try to get only the column with the type MOBILE - but remember that users are lazy and unlikely to label phone numbers properly. If more than one number exists, you should probably prompt the user to select the mobile number.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • This is only a suggestion but there is an option to select a number that is usually called when there are two or more numbers saved. You could use that information, and if not set, ask the user what to do – fkarg Dec 21 '14 at 08:36