我正在开发一款需要从联系人姓名中检索联系人号码的应用。我搜索了它并得到了代码“How to get a contact's number from contact name in android” 但问题是它只返回一个数字,即使有多个具有相同名称的条目。我希望我的代码返回所有具有相同名称的联系人的列表视图,然后用户可以选择他想要的任何一个。
答案 0 :(得分:0)
尝试使用装载程序
查看以下答案对我有用
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) {
}
}