1

I am reading contacts stored in phone and showing them in list view. My code is as follows:

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);
}

Now the problem it is reading the SIM contacts (and not phone contacts) also it is not reading all the contacts in SIM.

It is reading some of the contacts in SIM and exact number is 15 (while in SiM there are 100+ contacts) and also it repeats the contacts in list view.

like this

15 contacts then again the same 15 contacts are reapeating in list view.

again and again total 4 times 15 contacts are repeated.

3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.