-1

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 :)

Varun Vishnoi
  • 226
  • 6
  • 22

2 Answers2

0

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);
 }
Sri
  • 718
  • 1
  • 6
  • 21
  • You should write the custom adapter instead of using "SimpleCursorAdapter". Find in google write the custom adapter.this might helps http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ – Sri Jul 01 '13 at 07:46
  • I believe the `getId()` is just storing the id of the current person, and is probably found in the custom adapter. – SeanSWatkins Jul 01 '13 at 07:59
0

I don't know if you will find a tutorial on how to add images with a Simple adapter...you will probably need to create a custom list adapter.

it's really easy and gives you a huge amount of flexibility. try this that should give a decent starting point.

You will need to create a layout that has an ImageView, and the name, then in the adapter you can set the images and names etc.

SeanSWatkins
  • 413
  • 4
  • 9
  • Yes you are correct, I know how to code adapter but main problem is to find the fetching and setting way of contact images. – Varun Vishnoi Jul 01 '13 at 07:50
  • oh i see. I would assume you can just add into your projection String array the name of the image column and it would return the URI with the cursor? Otherwise you will have to re query the Contacts to get the URI form the id which you are getting from the Cursor returned by `getContacts()` – SeanSWatkins Jul 01 '13 at 08:02