Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

////////////////////////// XML ///////////////////////

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<QuickContactBadge
android:id=@+id/quickbadge
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleType="centerCrop"/>
...
</RelativeLayout>

////////////////////////// JAVA ////////////////////////////


(Set the Contact URI)

// The Cursor that contains contact rows

Cursor mCursor;

// The index of the _ID column in the Cursor

int mIdColumn;

// The index of the LOOKUP_KEY column in the Cursor


int mLookupKeyColumn;

// A content URI for the desired contact

Uri mContactUri;

// A handle to the QuickContactBadge view

QuickContactBadge mBadge;

...

mBadge = (QuickContactBadge) findViewById(R.id.quickbadge);

/*

* Insert code here to move to the desired cursor row

*/

// Gets the _ID column index

mIdColumn = mCursor.getColumnIndex(Contacts._ID);

// Gets the LOOKUP_KEY index

mLookupKeyColumn =
mCursor.getColumnIndex(Contacts.LOOKUP_KEY);

// Gets a content URI for the contact

mContactUri =

Contacts.getLookupUri(

mCursor.getLong(mIdColumn),

mCursor.getString(mLookupKeyColumn)

);

mBadge.assignContactUri(mContactUri);

/////////////
(Set the photo thumbnail)

Note: The PHOTO_THUMBNAIL_URI column isn't available in platform


versions prior to 3.0. For those versions, you must retrieve the URI from the
Contacts.Photo subtable.

// The column in which to find the thumbnail ID

int mThumbnailColumn;

/*

* The thumbnail URI, expressed as a String.

* Contacts Provider stores URIs as String values.

*/

String mThumbnailUri;

...

/*

* Gets the photo thumbnail column index if

* platform version >= Honeycomb

*/

if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.HONEYCOMB) {

mThumbnailColumn =

mCursor.getColumnIndex(Contacts.PHOTO_THUMBNAIL_URI);

// Otherwise, sets the thumbnail column to the _ID column

} else {

mThumbnailColumn = mIdColumn;
}

/*

* Assuming the current Cursor position is the contact you want,

* gets the thumbnail ID

*/

mThumbnailUri = mCursor.getString(mThumbnailColumn);

...

Add a QuickContactBadge to a ListView

/**

*/

private class ContactsAdapter extends CursorAdapter {

private LayoutInflater mInflater;

...

public ContactsAdapter(Context context) {

super(context, null, 0);

/*
* Gets an inflater that can instantiate

* the ListView layout from the file.

*/

mInflater = LayoutInflater.from(context);

...

...

/**

* Defines a class that hold resource IDs of each item layout

* row to prevent having to look them up each time data is

* bound to a row.

*/

private class ViewHolder {

TextView displayname;

QuickContactBadge quickcontact;

..

@Override

public View newView(

Context context,

Cursor cursor,

ViewGroup viewGroup) {

/* Inflates the item layout. Stores resource IDs in a


* in a ViewHolder class to prevent having to look

* them up each time bindView() is called.

*/

final View itemView =

mInflater.inflate(

R.layout.contact_list_layout,

viewGroup,

false

);

final ViewHolder holder = new ViewHolder();

holder.displayname =

(TextView) view.findViewById(R.id.displayname);

holder.quickcontact =

(QuickContactBadge)

view.findViewById(R.id.quickcontact);

view.setTag(holder);

return view;

...

@Override

public void bindView(

View view,

Context context,
Cursor cursor) {

final ViewHolder holder = (ViewHolder) view.getTag();

final String photoData =

cursor.getString(mPhotoDataIndex);

final String displayName =

cursor.getString(mDisplayNameIndex);

...

// Sets the display name in the layout

holder.displayname = cursor.getString(mDisplayNameIndex);

...

/*

* Generates a contact URI for the QuickContactBadge.

*/

final Uri contactUri = Contacts.getLookupUri(

cursor.getLong(mIdIndex),

cursor.getString(mLookupKeyIndex));

holder.quickcontact.assignContactUri(contactUri);

String photoData = cursor.getString(mPhotoDataIndex);

/*

* Decodes the thumbnail file to a Bitmap.

* The method loadContactPhotoThumbnail() is defined

* in the section "Set the Contact URI and Thumbnail"

*/
Bitmap thumbnailBitmap =

loadContactPhotoThumbnail(photoData);

/*

* Sets the image in the QuickContactBadge

* QuickContactBadge inherits from ImageView

*/

holder.quickcontact.setImageBitmap(thumbnailBitmap);

You might also like