Skip to content Skip to sidebar Skip to footer

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 tutori

Solution 1:

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()) {
                    returnnull; // no photo
                }
            } else {
                returnnull; // error in cursor process
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnnull;
        }
        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

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

Solution 2:

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.

Post a Comment for "Fetching All Contacts And Showing Them In Listview"