【发布时间】:2019-06-28 05:50:57
【问题描述】:
我正在开发一个管理联系人(添加、更新、删除)的 Android 应用程序
这是在列表视图中列出联系人的代码
list_ct.clear();
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursor != null)
{
while (cursor.moveToNext())
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor cursor2 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
ArrayList<String> phones = new ArrayList<String>();
while (cursor2.moveToNext())
{
String phoneNumber = cursor2.getString(cursor2.getColumnIndex(CommonDataKinds.Phone.NUMBER));
phones.add(phoneNumber);
}
Contact ct = new Contact(id,name,phones);
adapter.notifyDataSetChanged();
}
}
这是列表视图事件监听器
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Contact current = (Contact)list_ct.get(position);
Intent in=new Intent(Intent.ACTION_EDIT,Uri.parse("content://contacts/people/"+current.getId()));
startActivityForResult(in,2);
}
});
当我单击列表视图中的联系人时,其中一些人的意图会立即打开和关闭,就好像该 id 不存在于联系人的数据库中一样,而对于其他人,它会打开另一个联系人的联系人编辑。
联系人显示名称及其电话号码显示正确,但 ID 错误。我不明白为什么第一个光标中的 id 用于显示联系人的电话号码,但不能用于更新。
【问题讨论】:
标签: java android uri android-contentprovider android-contacts