The first part of our application could only display a default icon for each contact in our list. Now we want to change that and display the contact photo you added to your contacts on your phone.

First we start to change our main.xml. We have to remove the src attribute and add an id to the ImageView node.

1
2
3
<ImageView android:id="@+id/contact_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

On line 1 the source is replaced with the id named “contact_image”.

Next we need to extend the used SimpleCursorAdapter. Our class is simply named MySimpleCursorAdapter and we just want to override the bindView method. This method will be called for every row in our list.

1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
public void bindView(View view, Context context, Cursor cursor) {
    ImageView imageView = (ImageView) view.findViewById(R.id.contact_image);
 
    int id = _cursor.getColumnIndex(People._ID);
    Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, _cursor.getLong(id));
 
    Bitmap bitmap = People.loadContactPhoto(_context, uri, R.drawable.icon, null);
 
    imageView.setImageBitmap(bitmap);
 
    super.bindView(view, context, cursor);
}

Ok lets get through the code line by line.
First we need to get the ImageView so on line 3 we use the findViewById method to get it.
On line 5 we get the column index with the unique id of the contact.
Line 6 creates a uri appended with the id of the contact.
Line 8 loads the photo of the contact using this context, the created uri (created in line 6), a default icon and null as no decoding is needed/want.
On line 10 we set the bitmap to the ImageView object.
Line 12 called the overridden method to be sure the rest will be done, too.

The whole extended class should look like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.droidnova.android.contacts;
 
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
 
public class MySimpleCursorAdapter extends SimpleCursorAdapter {
    private Cursor _cursor;
    private Context _context;
 
    public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        _cursor = c;
        _context = context;
    }
 
    /**
     * {@inheritDoc}
     */
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ImageView imageView = (ImageView) view.findViewById(R.id.contact_image);
 
        int id = _cursor.getColumnIndex(People._ID);
        Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, _cursor.getLong(id));
 
        Bitmap bitmap = People.loadContactPhoto(_context, uri, R.drawable.icon, null);
 
        imageView.setImageBitmap(bitmap);
 
        super.bindView(view, context, cursor);
    }
}

Now your application displays the photo you once added to your contact or the default icon defined in the overridden method bindView.

Share