我使用过此代码,但结果不是我在listview中选择的名称的信息,而是来自其他名称的信息。
contactmanager.java
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, ContactEditor.class);
i.putExtra(ContactsContract.Contacts._ID, id);
startActivityForResult(i, 2);
}
ContactEditor.java
private Long mRowId;
mRowId = savedInstanceState != null ? savedInstanceState.getLong(ContactsContract.Contacts._ID):null;
if (mRowId == null){
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(ContactsContract.Contacts._ID):null;
}
populateFields();
private void populateFields(){
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (mRowId != null)
{
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
mContactNameEditText.setText(name);
}
}
}
答案 0 :(得分:0)
您必须询问您想要的ID。这会询问每个联系人,只显示最后一个:
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (mRowId != null)
{
尝试这样的事情:
if (mRowId != null)
{
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
ContactsContract.Contacts._ID + "=" + mRowId, null, null);