0

I am working on an app that requires retrieving contact's number from contact's name. i searched for it and got the code "How to get a contact's number from contact name in android" but the problem is that it just return only one number even though there are multiple entries with same name. i want my code to return a listview of all the contact that has same name and then user can choose whichever he wants.

Community
  • 1
  • 1
Saurav
  • 15
  • 7

1 Answers1

0

Try using loader

Check out following answer it work for me

String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
    String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;


public class SmsInviteFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_sms_invite, container, false);

   getActivity().getSupportLoaderManager().initLoader(1, null, SmsInviteFragment.this);
    return v;

}





@Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        onStartNetCall();
        Uri CONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

    return new CursorLoader(getActivity(), CONTENT_URI, null, null, null, null);
}

public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {


        String name = "" + cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
        String phoneNo = "" + cursor.getString(cursor.getColumnIndex(NUMBER));


        cursor.moveToNext();


    }

    Log.e(TAG, "onLoadFinished: ");


}

@Override
public void onLoaderReset(Loader<Cursor> loader) {

}

}
Sachin
  • 1,307
  • 13
  • 23