首页 > 其他分享 >QuickContactBadge的用法

QuickContactBadge的用法

时间:2022-12-09 17:35:26浏览次数:35  
标签:layout Contacts cache QuickContactBadge 用法 import android id


首先上个运行效果图:

QuickContactBadge的用法_null

 


下边是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);
}
}


标签:layout,Contacts,cache,QuickContactBadge,用法,import,android,id
From: https://blog.51cto.com/u_15907753/5926289

相关文章

  • OC之【NSString字符串的其他用法】
    #import<Foundation/Foundation.h>字符串的大小写处理voidNSString*str=@"GuangDong";//转成大写NSLog(@"大写:%@",[struppercaseString]);//转成小......
  • Objective-C #define 用法
    在C语言中,预处理代码(Preprocessor)是非常强大的工具,能让你的代码变得更加易读和易改。利用预处理代码,你可以重新定义代码的一部分,使得你的代码更适合你的风格。预处理......
  • OC之【@property的用法】
    1.这里的retain代表:在set方法中,release旧值,retain新值(nonatomic,retain)Book*book;(retain)Card*card;代表只生成get方法的声明默认是readwrite,同时生成get和set......
  • Linux基础知识(12)- GCC 简单使用(二)| Makefile 的高级用法
    在“Linux基础知识(11)-GCC简单使用(一)|GCC安装配置和Makefile的基本用法”里我们演示了GCC安装配置和Makefile的基本用法,本文将继续演示Makefile的高级用法。......
  • Linux基础知识(11)- GCC 简单使用(一)| GCC 安装配置和 Makefile 的基本用法
    GCC的全拼为GNUCCompiler,即GUN计划诞生的C语言编译器,显然最初GCC的定位确实只用于编译C语言。但经过这些年不断的迭代,GCC的功能得到了很大的扩展,它不仅可以用......
  • 一个很好的时间日期插件用法
    my97日期插件是一个非常好用、功能非常强大的日期控件,简单示例如下:静态限制注意:日期格式必须与realDateFmt和realTimeFmt一致而不是与dateFmt一致你可以给通过配......
  • dwr笔记二之经典用法之和spring结合+验证用户是否存在
    springmvc+DWR验证用户名是否存在,是最经典的案例了.1在DWR2里,注意配置的类名跟DWR1不同了由uk.ltd.getahead变成了org.directwebremoting。换上了新的配置1<ser......
  • springboot整合mongodb MongoTemplate和MongoRepository的用法
    前情Springboot是最简单的使用Spring的方式,而MongoDB是最流行的NoSQL数据库。两者在分布式、微服务架构中使用率极高,本文将用实例介绍如何在Springboot中整合MongoDB的两种......
  • python中input()、print()用法
    1.input()函数常涉及的强制类型转换第一种是在键入时进行转换,例如:a=int(input())1a=int(input())2b=int(input())3a=a+b4print(a)第二种则是在使用时进行转......
  • js Promise用法示例
    1.情景展示在前端js源码时,遇到了大量的Promise对象的用法,看得是一脸懵逼,Promise到底是个什么?2.具体分析在实际开发过程中,我们往往会遇到这样的场景:以ajax请求为例,我......