我有一个独立的应用程序,可以提取联系人的电话号码并将其填入文本框。这作为一个活动很好,但是当我将相同的代码放入一个片段时,我得到一个错误,它无法解析方法'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());
}
}
答案 0 :(得分:3)
我和你有同样的问题。
这是我的项目对话框,可以获取联系人信息并显示
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”,它与managedquery相同。你还需要一个像“我的代码中的对话框”这样的对话框名称来绑定getContentResolver()。查询(因为它不在一个活动上,而我的主类就像你的情况一样是片段)。抱歉英语不好
答案 1 :(得分:2)
如果您在managedQuery
中使用Fragment
,请在getActivity()
之前使用managedQuery
Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);