QuickContactBadge的用法

首先上个运行效果图:

下边是ListView 中单个Item的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:paddingLeft="0dip"
android:paddingRight="9dip"
android:layout_height= "wrap_content"
android:minHeight="48dip" android:id="@+id/contactItemRelLayout">

<QuickContactBadge
android:id="@+id/badge"
android:layout_marginLeft="2dip"
android:layout_marginRight="14dip"
android:layout_marginTop="4dip"
android:layout_marginBottom="3dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_height= "wrap_content"
android:layout_width= "wrap_content"
android:src="@drawable/default_head_pic"
style="?android:attr/quickContactBadgeStyleWindowSmall" />

<TextView
android:id="@+id/name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="2dip"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="哈哈"/>
<TextView
android:id="@+id/phone"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingRight="2dip"
android:layout_centerVertical="true"
android:paddingLeft="4dip"
android:layout_toRightOf="@id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="12345678901"/>
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:layout_alignParentRight="true"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"/>

</RelativeLayout>

下边是自定义的Adapter,这个很关键,和一般的Adapter不一样,不是继承自BaseAdapter

package abc.qiao.contact.adapter;

import java.util.ArrayList;
import java.util.List;

import abc.qiao.contact.R;
import android.content.Context;
import android.database.CharArrayBuffer;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.QuickContactBadge;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;

public class ContactAdapter extends ResourceCursorAdapter{
private Context context2;
static final int SUMMARY_ID_COLUMN_INDEX = 0;
static final int SUMMARY_NAME_COLUMN_INDEX = 1;
static final int SUMMARY_STARRED_COLUMN_INDEX = 2;
static final int SUMMARY_TIMES_CONTACTED_COLUMN_INDEX = 3;
static final int SUMMARY_PRESENCE_STATUS_COLUMN_INDEX = 4;
static final int SUMMARY_PHOTO_ID_COLUMN_INDEX = 5;
static final int SUMMARY_LOOKUP_KEY = 6;
static final int SUMMARY_HAS_PHONE_COLUMN_INDEX = 7;

private List<Long> contactIDList = new ArrayList<Long>();

public ContactAdapter(Context context, int layout, Cursor c) {
super(context, layout, c);
contactIDList.clear();
this.context2 = context;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
View view = super.newView(context, cursor, parent);
ContactListItemCache cache = new ContactListItemCache();
cache.nameView = (TextView)view.findViewById(R.id.name);
cache.photoView = (QuickContactBadge)view.findViewById(R.id.badge);
cache.phoneView = (TextView)view.findViewById(R.id.phone);
cache.checkBox = (CheckBox)view.findViewById(R.id.cb);
view.setTag(cache);
return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
final ContactListItemCache cache = (ContactListItemCache) view.getTag();
// Set the name
cursor.copyStringToBuffer(SUMMARY_NAME_COLUMN_INDEX, cache.nameBuffer);
int size = cache.nameBuffer.sizeCopied;
cache.nameView.setText(cache.nameBuffer.data, 0, size);
final long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
cache.phoneView.setText(getPhoneNumbersByContactID(contactId).toString());
// cache.checkBox.setChecked(false);
contactIDList.add(contactId);
final String lookupKey = cursor.getString(SUMMARY_LOOKUP_KEY);
cache.photoView.assignContactUri(Contacts.getLookupUri(contactId, lookupKey));
}

final static class ContactListItemCache {
public TextView nameView, phoneView;
public QuickContactBadge photoView;
public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);
public CheckBox checkBox;
}

public List<Long> getContactIdList(){
return this.contactIDList;
}
private String getPhoneNumbersByContactID(Long _id){
Cursor cursor = context2.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
+ Long.toString(_id), null, null);
String phoneNumber = "";
while(cursor.moveToNext()){
String strNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumber += strNumber + ",";
}
cursor.close();
String trailedPhoneNumber = phoneNumber.substring(0, phoneNumber.length() - 1);
return trailedPhoneNumber;
}

public void notifyall(){
notifyDataSetChanged();
}
}

下边是Activity的代码

package abc.qiao.contact;

import abc.qiao.contact.adapter.ContactAdapter;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.widget.ListView;

public class ContactSearchActivity extends Activity {
ListView listView = null;
ContactAdapter adapter = null;

static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
Contacts._ID, // 0
Contacts.DISPLAY_NAME, // 1
Contacts.STARRED, // 2
Contacts.TIMES_CONTACTED, // 3
Contacts.CONTACT_PRESENCE, // 4
Contacts.PHOTO_ID, // 5
Contacts.LOOKUP_KEY, // 6
Contacts.HAS_PHONE_NUMBER, // 7
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
initData();
}

private void init(){
listView = (ListView)findViewById(android.R.id.list);
listView.setBackgroundDrawable(getWallpaper());
listView.setCacheColorHint(00000000);//不加这个会出现滑动ListView时出现黑屏的现象
}

private void initData(){
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ Contacts.DISPLAY_NAME + " != '' ))";
Cursor c =
getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,
null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
adapter = new ContactAdapter(ContactSearchActivity.this, R.layout.contact_item_layout2, c);
listView.setAdapter(adapter);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值