我有一个独立的应用程序,可以提取联系人的电话号码并将其填写到文本框中。作为一项活动,它可以很好地工作,但是当我将相同的代码放入片段中时,出现一个错误,即它无法解析方法“ managedQuerry”。我也收到有关getContentResover()的消息。
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
try {
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cur = managedQuery(contactData, null, null, null, null);
ContentResolver contect_resolver = getContentResolver();
if (cur.moveToFirst()) {
String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = "";
String no = "";
Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
if (phoneCur.moveToFirst()) {
name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
Log.e("Phone no & name :***: ", name + " : " + no);
//txt.append(name + " : " + no + "\n");
editPhoneNum.setText(no);
id = null;
name = null;
no = null;
phoneCur = null;
}
contect_resolver = null;
cur = null;
// populateContacts();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
Log.e("IllegalArgException :: ", e.toString());
} catch (Exception e) {
e.printStackTrace();
Log.e("Error :: ", e.toString());
}
}
最佳答案
我和你有同样的问题。
这是我项目的对话框,可获取联系人信息并显示给他们
private void contactsdialog() {
final Dialog dialog = new Dialog(getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.contactsdialog);
ListView contacts_list = (ListView)dialog.findViewById(R.id.listView);
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID
};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cursor =dialog.getContext().getContentResolver().query(uri, projection, null, null, sortOrder);
ArrayList<String> columnArray1 = new ArrayList<String>();
ArrayList<String> columnArray2 = new ArrayList<String>();
while (cursor.moveToNext()) {
try {
columnArray2.add(cursor.getString(0));
columnArray1.add(cursor.getString(1));
} catch(Exception e) {
// Log.d("error",e.toString());
}
}
cursor.close();
String[] colStrArr1 = (String[]) columnArray1.toArray(new String[columnArray1.size()]);
String[] colStrArr2 = (String[]) columnArray2.toArray(new String[columnArray1.size()]);
contactadapter adapter = new contactadapter(getActivity(),colStrArr1,colStrArr2);
contacts_list.setAdapter(adapter);
dialog.show();
}
如您所见,我不使用managedquery,因为它已被弃用。相反,我使用了与托管查询相同的“ getContentResolver()。query”。另外,您还需要一个对话框名称(如“我的代码中的对话框”)将它与getContentResolver()。query绑定(因为它不在活动中,并且我的主类是类似于您的情况的片段)。对不起,英语不好