-1

I am developing an application in that I have to search numbers from contacts and add that contact to my edit text. I wrote some code it go to contacts but not add in edit text please tell me any mistake in my code

my code

  searchBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

            intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            startActivityForResult(intent,1);
        }
    });

     @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == RESULT_OK){
         Uri contactData = data.getData();
         Cursor cursor = managedQuery(contactData, null, null, null, null);
         cursor.moveToFirst();
         String number = cursor.getString(cursor.getColumnIndexOrThrow
                 (ContactsContract.CommonDataKinds.Phone.NUMBER));

        //contactName.setText(name);
        ephoneNumber.setText(number);
        //contactEmail.setText(email);
    }
}
Simulant
  • 19,190
  • 8
  • 63
  • 98
DurgaPrasad Ganti
  • 1,008
  • 4
  • 20
  • 41

2 Answers2

4

You can use ContentResolver to do this. Call this method to get the contact name:

public String getContactDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String name = "?";

ContentResolver contentResolver = getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,
        ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

try {
    if (contactLookup != null && contactLookup.getCount() > 0) {
        contactLookup.moveToNext();
        name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
        //String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
    }
} finally {
    if (contactLookup != null) {
        contactLookup.close();
    }
}

return name;
}

This method will return the Name of your desired number from contact list. The code reference is this question

Community
  • 1
  • 1
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
0

you can fetch name or number both

// fetch contact name from phone directory
    public String fetchNamePhone(String number){

        String numberName= null;

        /// number is the phone number
        Uri lookupUri =     Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(number));

        String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };                                                                                  

        Cursor c = getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null,null);

        try {
            if(c.moveToFirst()){

                 return number;
                // numberName =      c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
            }
           else{
              number = "";
           }
        } catch (Exception e) {
            // TODO: handle exception
        }

        return number;
    }

After call this function , you will check number.length if greater then 1 then number is available otherwise number is not available