1
我正在閱讀存儲在手機中的聯繫人並在列表視圖中顯示他們。 我的代碼如下:不能讀取手機聯繫人
String col[]={ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
Cursor cursorNames = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, col, null, null, null);
intentContactsToreturn=new Intent();
ArrayList<String> contactNames=new ArrayList<String>();
ArrayList<String> contactNumbers=new ArrayList<String>();
try
{
if(cursorNames.getCount() > 0)
{
while(cursorNames.moveToNext())
{
String id = cursorNames.getString(cursorNames.getColumnIndex(Contacts._ID));
contactNames.add(cursorNames.getString(cursorNames.getColumnIndex(Contacts.DISPLAY_NAME)));
Cursor cursorNumbers =getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
cursorNumbers.moveToFirst();
contactNumbers.add(cursorNumbers.getString(cursorNumbers.getColumnIndex(CommonDataKinds.Phone.NUMBER)));
}
}
}
finally
{
cursorNames.close();
}
ContactListArrayAdapter arrayAdapter = new ContactListArrayAdapter(this,contactNames,contactNumbers);
// lv.addFooterView(b);
listViewContacts.setAdapter(arrayAdapter);
}
現在的問題是讀取SIM卡聯繫人(而不是手機中的聯繫人),也它不讀SIM卡中的所有聯繫人。
它讀取SIM中的一些聯繫人,確切的數字是15(而在SiM中有100多個聯繫人),並且它在列表視圖中重複聯繫人。
這樣
15接觸 然後再同15個觸點reapeating列表視圖。
一次又一次共重複4次15次接觸。
嘗試在您開始遍歷遊標之前調用cursor.moveToFirst,也許這將解決問題。 – Egor 2012-03-21 06:02:17
希望您添加了READ_CONTACTS權限。 – 2012-03-21 06:03:17
[此處輸入鏈路描述] [1] [1]:http://stackoverflow.com/questions/2356084/read-all-contacts-phone-numbers-in-android – Android 2012-03-21 06:21:26