android提供了本地数据库的查询uri,能够查询出数据:

採用一个AsyncQueryHandler来进行查询, AsyncQueryHandler自己开启了线程来进行数据查询,非常方便

protected AsyncQueryHandler mQueryHandler;

protected final void queryPersonal() {
mQueryHandler.startQuery(QUERY_TOKEN, null, ContactsContract.Contacts.CONTENT_URI,
Personal.CONTACTS_SUMMARY_PROJECTION, null, null, getSortOrder(ContactsContract.Contacts.DISPLAY_NAME));
} protected static String getSortOrder(String fieldName) {
//substr为截取函数。取第一个字母
//COLLATE主要用于对字符进行排
//COLLATE LOCALIZED 按本地语言进行排序
return "CASE WHEN substr(UPPER(" + fieldName + "), 1, 1) BETWEEN 'A' AND 'Z' THEN 1 else 10 END," +
fieldName + " COLLATE LOCALIZED ASC";
} protected final class MyHandler extends AsyncQueryHandler {
/**
* Asynchronous query handler constructor.
*/
public MyHandler(Context context) {
super(context.getContentResolver());
} /**
* On query completion.
*/
@Override
<strong>protected void onQueryComplete(int token, Object cookie, Cursor cursor) </strong>{ //handler查询完的回调
if (cursor == null || cursor.isClosed()) {
return;
}
if (!isFinishing()) {
setLoading(false);
if (mAdapter != null) {
mAdapter.setLoading(false);
mAdapter.changeCursor(cursor);
} if (cursor.getCount() == 0) {
mEmtytext.setVisibility(View.VISIBLE);
} else {
mEmtytext.setVisibility(View.INVISIBLE);
}
} else {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
}
}

这个Layout是:

<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:rcm="http://schemas.android.com/apk/res/com.ringcentral.android"
android:id="@+id/contact_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bgColorMain"
android:orientation="vertical"> <ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@android:color/transparent"
android:descendantFocusability="afterDescendants"
android:divider="@null"
android:fastScrollEnabled="true"
android:listSelector="@drawable/bg_list_item_selector" /> <RelativeLayout
android:id="@+id/no_contact_indication"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/emptyListText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:singleLine="true"
android:text="No Contacts"
android:textColor="@color/text_no_items"
android:textSize="20sp" /> <ProgressBar
android:id="@+id/loading"
style="@style/RCMProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout> </LinearLayout>

然后是list adapter的写法:

	protected static final class SectionedContactListItemCache {
public TextView sectionHeader;
public TextView nameView;
public TextView typeView;
public ImageView photoView;
public ImageView detailItemIcon;
<span style="white-space:pre"> </span>public View nameTypeWrapper;
} protected final class ContactsAdapter extends ResourceCursorAdapter { public ContactsAdapter(Context context) {
super(context, R.layout.contacts_list_item_photo,null);
} @Override
public void changeCursor(Cursor c) {
super.changeCursor(c);
} protected String getTitle(String displayName) {
String title;
/** check if the first letter is English letter */
Matcher matcher = mPattern.matcher(displayName);
if (!matcher.find()) {
title = NONE_ENGLISH_LETTER_TITLE;
} else {
title = displayName.trim().substring(0, 1).toUpperCase(Locale.US);
}
return title;
} protected String getDisplayName(Cursor c) { String displayName = c.getString(Personal.NAME_COLUMN_INDEX); if(TextUtils.isEmpty(displayName)) {
return "";
} return displayName;
} @Override
public void bindView(View view, Context context, Cursor cursor) {
final SectionedContactListItemCache cache = (SectionedContactListItemCache) view.getTag();
cache.typeView.setVisibility(View.GONE);
cache.photoView.setVisibility(View.VISIBLE);
String name = cursor.getString(Personal.NAME_COLUMN_INDEX);
if (TextUtils.isEmpty(name)) {
cache.nameView.setText(R.string.contact_no_name);
} else {
cache.nameView.setText(name);
}
} @Override
public View newView(Context context, Cursor cursor, ViewGroup parent) { View view = super.newView(context, cursor, parent);
final SectionedContactListItemCache cache = new SectionedContactListItemCache();
cache.nameTypeWrapper = view.findViewById(R.id.name_type);
cache.sectionHeader = (TextView) view.findViewById(R.id.txtSectionHeader);
cache.nameView = (TextView) view.findViewById(R.id.name);
cache.typeView = (TextView) view.findViewById(R.id.type);
cache.photoView = (ImageView) view.findViewById(R.id.photo);
cache.detailItemIcon = (ImageView) view.findViewById(R.id.contacts_detail_item_icon);
view.setTag(cache); return view;
} }//end of adapter

item adapter的layout:

<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:id="@+id/txtSectionHeader"
android:layout_width="match_parent"
android:layout_height="@dimen/list_item_section_height"
android:background="@drawable/bg_contacts_section_header"
android:gravity="center_vertical|left"
android:paddingLeft="@dimen/default_padding_to_side"
android:textColor="@color/contacts_text_separator_text_color"
android:textSize="@dimen/font_size_medium" /> <RelativeLayout
android:id="@+id/contact_item"
android:layout_width="match_parent"
android:layout_height="@dimen/general_list_view_item_height"> <ImageView
android:id="@+id/photo"
android:layout_width="@dimen/favorites_item_picture_width"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignWithParentIfMissing="true"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:layout_marginLeft="@dimen/contact_photo_margin_left_right"
android:scaleType="fitCenter"
/> <!-- this icon may be added in next version, and now it would be hidden --> <ImageView
android:id="@+id/contacts_detail_item_icon"
android:layout_width="50dip"
android:layout_height="60dip"
android:layout_alignParentRight="true"
android:layout_marginRight="0dip"
android:cropToPadding="true"
android:duplicateParentState="false"
android:paddingBottom="3dip"
android:paddingTop="3dip"
android:scaleType="fitCenter"
android:src="@drawable/ic_list_link"
android:visibility="gone" /> <RelativeLayout
android:id="@+id/name_type"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignWithParentIfMissing="true"
android:layout_toLeftOf="@id/contacts_detail_item_icon"
android:layout_marginLeft="@dimen/contact_photo_margin_left_right"
android:layout_toRightOf="@id/photo"
android:background="@drawable/bg_list_item_divider"> <TextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/name"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/contact_type_margin_left_right"
android:ellipsize="marquee"
android:gravity="center_vertical|right"
android:singleLine="true"
android:textColor="@color/refresh_control_text_color"
android:textSize="@dimen/font_size_small" /> <TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/contact_name_margin_left_right"
android:layout_toLeftOf="@id/type"
android:ellipsize="marquee"
android:gravity="center_vertical|left"
android:singleLine="true"
android:textColor="@color/text_color_main"
android:textSize="@dimen/font_size_medium" />
</RelativeLayout>
</RelativeLayout> </LinearLayout>

怎样获取android手机联系人并按字母展示(一)的更多相关文章

  1. 怎样获取android手机联系人并按字母展示(三)

    假设获取contact的头像信息并展示: 怎样依据photoId来获取bitmap: public static Bitmap getContactPhoto(Context context, lon ...

  2. 获取android手机联系人信息

    package com.yarin.android.Examples_04_04; import android.app.Activity; import android.database.Curso ...

  3. 获取Android 手机屏幕宽度和高度以及获取Android手机序列号

    1.获取Android 手机屏幕宽度 1 DisplayMetrics dm = new DisplayMetrics(); 2 this.getWindowManager().getDefaultD ...

  4. 关于Android的Build类——获取Android手机设备各种信息

    经常遇到要获取Android手机设备的相关信息,来进行业务的开发,比如经常会遇到要获取CPU的类型来进行so库的动态的下载.而这些都是在Android的Build类里面.相关信息如下: private ...

  5. Pyqt adb 获取Android手机屏幕

    adb的全称为Android Debug Bridge,就是起到调试桥的作用.adb的工作方式比较特殊,采用监听Socket TCP 5554等端口的方式让IDE和Qemu通讯,默认情况下adb会da ...

  6. 获取android手机基本信息

    /** * 获取android当前可用内存大小 */ private String getAvailMemory() {// 获取android当前可用内存大小 ActivityManager am  ...

  7. 如何获取Android手机的唯一标识

    有很多场景和需求你需要用到手机设备的唯一标识符. 在Android中,有以下几种方法获取这样的ID. 1. The IMEI: 仅仅只对Android手机有效: 1 2 TelephonyManage ...

  8. (转)获取android手机内部存储空间和外部存储空间的参数 && 如何决定一个apk的安装位置

    转:http://blog.csdn.net/zhandoushi1982/article/details/8560233 获取android文件系统的信息,需要Environment类和StatFs ...

  9. 如何获得android手机通讯录的字母显示(两)

    随后的写如何使各第一字母显示相同的分类触点: 于adapter implement SectionIndexer 这项adapter必须在下面可以实现3接口: @Override public Obj ...

随机推荐

  1. uva 11475 - Extend to Palindrome(KMP)

    option=com_onlinejudge&Itemid=8&category=506&page=show_problem&problem=2470" ta ...

  2. svn跨机备份

    #!/bin/sh svn_bak_dir='/svndata/cloudil' svn_server='svn://172.16.40.200:9999' user=adminread pass=a ...

  3. 如何高效地向Redis插入大量的数据(转)

    最近有个哥们在群里问,有一个日志,里面存的是IP地址(一行一个),如何将这些IP快速导入到Redis中. 我刚开始的建议是Shell+redis客户端. 今天,查看Redis官档,发现文档的首页部分( ...

  4. _tcscat在Debug和Release根据问题

    背景: 因此,例如,在下面的代码段,作用是得到的路径当前程序(C:\work\A.exe),然后"A.exe"拆除,组装的"C:\work\inject.dll" ...

  5. Android 的Google+平台

    Google+是谷歌推出的身份服务和社交网站.也是Google各种服务社交层面的补强.是世界上第二大的社交网站.一旦用户登录到Google,你就可以按照自己的需要定制服务和使用你的应用程序.

  6. 启动和关闭JBoss As 7.1.1脚本

    启动和关闭JBoss As 7.1.1,脚本例如以下djboss.sh: #!/bin/sh #JBOSS_HOME JBOSS_HOME=/opt/jboss case "$1" ...

  7. hdu 5071 Chat(模拟)

    题目链接:hdu 5071 Chat 题目大意:模拟题. .. 注意最后说bye的时候仅仅要和讲过话的妹子说再见. 解题思路:用一个map记录每一个等级的妹子讲过多少话以及是否有这个等级的妹子.数组A ...

  8. Urban Dictionary: psd

    Urban Dictionary: psd psd Share on twitter Share on facebook Share on more 3 up, 1 down It means Poo ...

  9. 怎样用Google APIs和Google的应用系统进行集成(1)----Google APIs简介

    Google的应用系统提供了非常多的应用,比方 Google广告.Google 任务,Google 日历.Google blogger,Google Plus,Google 地图等等非常的多的应用,请 ...

  10. 手机游戏产品经理(一)logo的印象非常重要,以促进

    从事的工作有一段时间的产品,在产品上共享所以现在的一些经验和知识,并记录.首先,我现在做国外casino手游,如此专注casino展开游戏的主题. 首先说一款游戏的logo非常重要,假设设计的好.它能 ...