Tuesday, 20 August 2013

Issue in Adding Cursor Values to ArrayList

Issue in Adding Cursor Values to ArrayList

I am trying to query the default phone content provider's Phone Numbers
and storing the values thus obtained into an 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();
}
However this gives array out of bounds error at the Log.d method. For some
reason only one value is getting stored insidei.e. I only get ar.get(0) as
a valid output. What mistake am i making??

No comments:

Post a Comment