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.

Ibrahim Azhar Armar
  • 24,538
  • 34
  • 126
  • 199
kamal
  • 2,368
  • 2
  • 27
  • 35

1 Answers1

1

I used this code to get the contacts.

    public ArrayList<String> c_Name = new ArrayList<String>();
    public ArrayList<String> c_Number = new ArrayList<String>();
    static String[] name_Val = null;
    String[] phone_Val = null;
    String local_value = null;

ContentResolver cr1 = getContentResolver();
        String[] projection = new String[] { People._ID, People.NAME,
                People.NUMBER };
        Uri phone_contacts = People.CONTENT_URI;
        Cursor managedCursor = cr1.query(phone_contacts, projection, null,
                null, People.NAME + " ASC");
        if (managedCursor.moveToFirst()) {
            String contactname;
            String cphoneNumber;
            int nameColumn = managedCursor.getColumnIndex(People.NAME);
            int phoneColumn = managedCursor.getColumnIndex(People.NUMBER);
            Log.d("int Name", Integer.toString(nameColumn));
            Log.d("int Number", Integer.toString(phoneColumn));
            do {

                contactname = managedCursor.getString(nameColumn);
                cphoneNumber = managedCursor.getString(phoneColumn);
                Log.d("Outside cname", "ts" + contactname);
                Log.d("Outside cno", "ts" + cphoneNumber);
                if ((contactname == " " || contactname == null)
                        && (cphoneNumber == " " || cphoneNumber == null)) {
                    // displayAlert1();

                } else {
                    c_Name.add(contactname);
                    c_Number.add(cphoneNumber);
                    Log.d("contact Name", c_Name.toString());
                    Log.d("contact No", c_Number.toString());
                }
            } while (managedCursor.moveToNext());

        }

        Uri contacts = Uri.parse("content://icc/adn");

        Cursor managedCursor1 = cr1.query(contacts, null, null, null, null);

        if (managedCursor1.moveToFirst()) {

            String contactname;
            String cphoneNumber;

            int nameColumn = managedCursor1.getColumnIndex("name");
            int phoneColumn = managedCursor1.getColumnIndex("number");

            Log.d("int Name", Integer.toString(nameColumn));
            Log.d("int Number", Integer.toString(phoneColumn));

            do {
                // Get the field values
                contactname = managedCursor1.getString(nameColumn);
                cphoneNumber = managedCursor1.getString(phoneColumn);
                if ((contactname == " " || contactname == null)
                        && (cphoneNumber == " " || cphoneNumber == null)) {
                    // displayAlert1();

                } else {
                    c_Name.add(contactname);
                    c_Number.add(cphoneNumber);
                }
            } while (managedCursor1.moveToNext());

        }
        name_Val = (String[]) c_Name.toArray(new String[c_Name.size()]);
        phone_Val = (String[]) c_Number.toArray(new String[c_Name.size()]);

Then set the name_Val, phone_Val to your listview.

             ContactListArrayAdapter arrayAdapter = new ContactListArrayAdapter(this,name_Val,phone_Val );
   // lv.addFooterView(b);
   listViewContacts.setAdapter(arrayAdapter);
Manikandan
  • 2,463
  • 5
  • 51
  • 91