号码中的联系人 ID
我可以从她的 ID 中找到联系人的电话号码。以下代码片段在屏幕上打印一个数字。
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone._ID +" = 4627",
null, null);
phones.moveToNext();
String phoneNumber = phones.getString(
phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getBaseContext(), phoneNumber, Toast.LENGTH_LONG).show();
但是,如果我尝试从她的号码中找到她的 ID,
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone._ID +" = 4627",
null, null);
phones.moveToNext();
String phoneNumber = phones.getString(
phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Now I got a valid phone number
//using that number to find the id
Cursor ids = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone.NUMBER +" = "+ phoneNumber,
null, null);
ids.moveToNext();
String id = ids.getString(
ids.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
Toast.makeText(getBaseContext(), id, Toast.LENGTH_LONG).show();
则会收到以下错误。
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
我使用的投影如下:
private static final String[] CONTACT_PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID
};
即使我知道联系人中有这样的条目,第二个光标也是空的。我做错了什么?
I can find the number of the contact from her id. The following snippet prints a number on the screen.
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone._ID +" = 4627",
null, null);
phones.moveToNext();
String phoneNumber = phones.getString(
phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getBaseContext(), phoneNumber, Toast.LENGTH_LONG).show();
However, if I try to find her id from her number,
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone._ID +" = 4627",
null, null);
phones.moveToNext();
String phoneNumber = phones.getString(
phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Now I got a valid phone number
//using that number to find the id
Cursor ids = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
CONTACT_PROJECTION,ContactsContract.CommonDataKinds.Phone.NUMBER +" = "+ phoneNumber,
null, null);
ids.moveToNext();
String id = ids.getString(
ids.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
Toast.makeText(getBaseContext(), id, Toast.LENGTH_LONG).show();
I get the following error.
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
The projection I use is as follows:
private static final String[] CONTACT_PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID
};
The second cursor is empty even if I know there is such an entry in the contacts. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道为什么,但 PhoneLookup 对我有用。对于那些有同样问题的人,这是我的代码。
Don't know why, but PhoneLookup worked for me. For those who have the same problem, here is my code.