我正在尝试查询默认电话内容提供商的电话号码,并将如此获得的值存储到ArrayList中 ar = new ArrayList(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
if (cur.getCount() > 0){
while (cur.getCount() > 0){
while(cur.moveToNext()){
//unique id
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
//number corresponding to the id
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//lets check if this guy even has a number saved
if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){
//Query here
//Get the phone numbers!
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
new String[]{id},null);
while(pCur.moveToNext()){
ar.add((pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))));
}
Log.d("String Report", ar.get(5));
pCur.close();
}
但是,这会在Log.d方法中给出数组越界错误。出于某种原因,只有一个值被存储在内部。我只获得ar.get(0)作为有效输出。我犯了什么错误?
答案 0 :(得分:0)
在
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
你必须这样做
cur.moveToNext();
还
pCur.moveToNext();
在尝试使用它之前!
这将光标移动到第一个元素