Android的内容提供商返回错误的接触式ID

问题描述 投票:0回答:1

我工作的管理联系人(添加,更新,删除)的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);
    }
});

当我点击在ListView中的联系人,他们当中有些人的意图打开和关闭立刻,仿佛ID不会在联系人的数据库中存在,并为他人它打开了另一个接触接触编辑。

联系人显示名称和电话号码显示正确,但ID是错误的。我不明白的是为什么在第一光标的ID适用于显示该联系人的电话号码,但不更新。

java android uri android-contentprovider android-contacts
1个回答
0
投票

您正在使用一种名为“人”为您的联系URI一个非常古老和过时的Android API,千万不要用人民的API,总是ContactsContract。

您应该建立您的联系URI是这样的:

Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);

其他然后,在你的查询是超级慢,可以取代那些数以百计的查询与一个快速的查询,让您的应用更快,更轻松的生活,看到这个答案的示例代码:https://stackoverflow.com/a/47788324/819355

© www.soinside.com 2019 - 2024. All rights reserved.