gpt4 book ai didi

android - 在检索联系电话时使用 getColumnIndex() 获取 -1 - Android

转载 作者:行者123 更新时间:2023-11-29 16:03:56 27 4
gpt4 key购买 nike

我正在尝试检索联系人的号码。我可以毫无问题地访问联系人姓名,但是当我尝试访问联系人号码时出现异常,因为 getColumnIndex 返回 -1,这意味着它找不到我请求的列。但是我不明白为什么当我从 Android 文档中看到该列确实存在时它找不到该列。

这是我的代码:

public ArrayList<String> getContactById(String contactId)
{
Log.d("XYZ", "Contact id -> " + contactId);
ArrayList<String> contact = new ArrayList<String>();
ContentResolver cr = context.getContentResolver();
final String[] projection = new String[] {
Contacts.DISPLAY_NAME, // the name of the contact
Contacts.PHOTO_ID // the id of the column in the data table for the image
};

final Cursor curContact = cr.query(
Contacts.CONTENT_URI,
projection,
Contacts._ID + "=?", // filter entries on the basis of the contact id
new String[]{String.valueOf(contactId)}, // the parameter to which the contact id column is compared to
null);

if(curContact.moveToFirst()) {
final String name = curContact.getString(curContact.getColumnIndex(Contacts.DISPLAY_NAME));
final String photoId = curContact.getString(curContact.getColumnIndex(Contacts.PHOTO_ID));
Log.d("XYZ", "Name -> " + name);
Log.d("XYZ", "Photo id -> " + photoId);
}



// Get the phone numbers

final String[] phoneNumbersProjection = new String[] {
Phone.NUMBER,
Phone.TYPE,
};

final Cursor curPhone = cr.query(
Phone.CONTENT_URI,
projection,
Data.CONTACT_ID + " = " + contactId, null,
//new String[]{String.valueOf(contactId)},
null);

if(curPhone.moveToFirst()) {
final int contactNumberColumnIndex = curPhone.getColumnIndex(Phone.NUMBER);
final int contactTypeColumnIndex = curPhone.getColumnIndex(Phone.TYPE);

Log.d("XYZ", "contactNumberColumnIndex -> " + contactNumberColumnIndex);
Log.d("XYZ", "contactTypeColumnIndex -> " + contactTypeColumnIndex);

while(!curPhone.isAfterLast()) {
final String number = curPhone.getString(contactNumberColumnIndex);
final int type = curPhone.getInt(contactTypeColumnIndex);
final int typeLabelResource = Phone.getTypeLabelResource(type);
Log.d("XYZ", "Contact number -> " + number);
//Log.d("XYZ", "Number Type -> " + typeLabelResource);
//doSomethingWithAContactPhoneNumber(number, typeLabelResource);
curPhone.moveToNext();
}

}
curPhone.close();
curContact.close();
return contact;
}

与:

Log.d("XYZ", "contactNumberColumnIndex -> " + contactNumberColumnIndex);
Log.d("XYZ", "contactTypeColumnIndex -> " + contactTypeColumnIndex);

我收到:

contactNumberColumnIndex -> -1
contactTypeColumnIndex -> -1

谁能找出我的代码有什么问题?

谢谢!

最佳答案

你使用了错误的投影:

final String[] phoneNumbersProjection = new String[] {
Phone.NUMBER,
Phone.TYPE,
};

final Cursor curPhone = cr.query(
Phone.CONTENT_URI,
projection,

您应该使用 phoneNumbersProjection 而不是您之前用于另一个查询的 projection

通常,getColumnIndex() 会尝试从 Cursor 中的列中查找列,因此如果未找到列,请首先检查您是否将该列包含在首先查询。

关于android - 在检索联系电话时使用 getColumnIndex() 获取 -1 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21255636/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com