Read all contacts from phone and phone

EDIT: The code works. This was a problem with Eclipse, and the code displays the output in logcat, as intended.

Android 2.3.3

I am new to using content providers. I just want to try an example of extracting contacts from my phone. I saw some examples, but no one worked for me when I tried this through my SAMSUNG Mobile.

Here is the code I used ...

public class Class_Add_Contact extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add_contact);

        readContacts();    
    }

    private void readContacts() {
        // TODO Auto-generated method stub

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);

        while (cur.moveToNext()) {

            String name =cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));    
            String phoneNumber = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            System.out.println(name + "   " + phoneNumber);
            }
    }
}

The code seems fine, but the numbers are not displayed in the logarithm. What could be wrong?

+5
source share
1 answer

try it

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 

while (cursor.moveToNext()) {
    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    System.out.println(contactId + "   " + name);
}
cursor.close();

it returns the contact name and identifier (identifier, probably the row number of the contact table)

+6
source

All Articles