如何以编程方式在列表视图中获取Android手机通讯录我的代码

时间:2014-04-11 10:22:52

标签: android sqlite

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

View view=View.inflate(context, R.layout.main, null);
ListView list = (ListView)view. findViewById(android.R.id.list);
String[] columns = new String[{ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.PhoneLookup.NUMBER};

int[] views = new int[] {R.id.contactName, R.id.contactID};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(context,R.layout.text,cur,columns, views);

list.setAdapter(adapter);   

1 个答案:

答案 0 :(得分:2)

this is real example
public Cursor getAllContacts() {
            Uri uri = ContactsContract.Contacts.CONTENT_URI;
            String[] projection = new String[] {
                    ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.LOOKUP_KEY,
                    ContactsContract.Contacts.DISPLAY_NAME, 
                    ContactsContract.Contacts.DISPLAY_NAME_ALTERNATIVE
            };
            String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'";
            String[] selectionArgs = null;
            String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

            return getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
        }