Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

android - Fetching all Contacts and showing them in listview

I am showing all contacts in listview and it is working great. But I also want to add image to listview. Searched alot but didn't find any good tutorial. Please suggest some tutorials for showing contact images on listview. Following is my code.

Cursor cur = getContacts();

    ListView lv = getListView();

    String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME };


    adapter = new SimpleCursorAdapter(this,
            R.layout.contacts_list_row, cur, fields,
            new int[] { R.id.title}, 0);
    lv.setAdapter(adapter);

getContacts()

private Cursor getContacts() {
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;

    String[] projection = new String[] { ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME };
    String selection = null;
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
            + " COLLATE LOCALIZED ASC";
    return managedQuery(uri, projection, selection, selectionArgs,
            sortOrder);
}

Thanks in advance :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Write a custom list view with ImageView and TextView and get the contact icon image from the Content provider using the bellow code

   /**
     * @return the photo URI
     */
    public Uri getPhotoUri() {
        try {
            Cursor cur = this.ctx.getContentResolver().query(
                    ContactsContract.Data.CONTENT_URI,
                    null,
                    ContactsContract.Data.CONTACT_ID + "=" + this.getId() + " AND "
                            + ContactsContract.Data.MIMETYPE + "='"
                            + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,
                    null);
            if (cur != null) {
                if (!cur.moveToFirst()) {
                    return null; // no photo
                }
            } else {
                return null; // error in cursor process
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long
                .parseLong(getId()));
        return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    }

And then check the condition image is available then set to imageview

 Uri u = objItem.getPhotoUri();
 if (u != null) {
    mPhotoView.setImageURI(u);
 } else {
    mPhotoView.setImageResource(R.drawable.defaultimage);
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...