我一直在关注this网站上的教程,以获取Android中联系人的电话号码。
我面临的问题是,我似乎无法获得最后一个联系电话。 例如,如果我保存了3个联系人,则会显示前2个的编号。
我发布我的代码,如果它可以帮助任何人。
if(cur.getCount()>0){
while(cur.moveToNext()){
String id=cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))>0){
pCur=getContentResolver().
query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone._ID+"=?",
new String[]{id}, null);
while(pCur.moveToNext()){
number.add(pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
}
pCur.close();
}
}
任何帮助将不胜感激。 谢谢!
============================
编辑:对于下面感兴趣的人,代码对我有用:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("Details", "Name: "+name+" Number: "+phoneNumber);
}
phones.close();
答案 0 :(得分:0)
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
String selection = ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER;
+
String[] selectionArgs = new String[] { "1" };
Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
selection,
selectionArgs, null}
if (c.moveToFirst())
{
do
{
Log.d("Test", "Contact: id = " + c.getLong(0) + " name = " + c.getString(1) + " phone = " + c.getString(2));
}
}
答案 1 :(得分:0)
您可以尝试使用以下代码获取所有联系人
Cursor c=dbHelper.getAllContact();
startManagingCursor(c);
ArrayList<String> columnArray1 = new ArrayList<String>();
if (c.moveToFirst())
{
do {
columnArray1.add(c.getString(0));
} while (c.moveToNext());
}