DBMNG数据库管理与应用

书籍是全世界的营养品。生活里没有书籍,就好像没有阳光;智慧里没有书籍,就好像鸟儿没有翅膀。
当前位置:首页 > 移动应用 > Android

Android通讯录中联系人按姓名排序(中文)

package com.dbmng.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

/*
 * 获取通讯录信息
 */
public class Tabpg2GetContactInfoGroup
{
/*
* 根据输入号码部分模糊查询获取两个通讯录信息数据
*/
Activity activity;


public Tabpg2GetContactInfoGroup(Activity activity1)
{
activity = activity1;
}


public List<Map<String, Object>> getListData()
{
int theSDKversion = SystemIps.getSDKVersionNumber();// 获取android版本号
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
if (theSDKversion > 7)//>adroid2.1
{
// 快速读取联系人信息
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[]
{ "sort_key", ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER };
String sortOrder = ""// ContactsContract.Contacts.DISPLAY_NAME
+ " sort_key  COLLATE LOCALIZED  ASC";
ContentResolver cr = activity.getContentResolver();
Cursor cursor = cr.query(uri, projection, null, null, sortOrder);
while (cursor.moveToNext())
{
String sortKeyString = cursor.getString(0);
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


map = new HashMap<String, Object>();
map.put("ContactSortKey", sortKeyString.substring(0, 1).toUpperCase());
map.put("ContactName", name);
map.put("ContactTel", phoneNum);
map.put("ContactPhoto", R.drawable.adrlook);
list.add(map);//逐条记录插入
}
try
{
// 4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15)
//if (theSDKversion < 14)
{
cursor.close();
}
} catch (Exception e)
{
}
} else
{
// 快速读取联系人信息
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[]
{ ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER };
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
String searchRularString = null;// 查询规则,若输入号码字符串不为空,则赋值;否
ContentResolver cr = activity.getContentResolver();
Cursor cursor = cr.query(uri, projection, searchRularString, null, sortOrder);
while (cursor.moveToNext())
{
String sortKeyString = this.toPinYin(cursor.getString(1));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
map = new HashMap<String, Object>();
map.put("pinyin", sortKeyString);//仅用作下面紧接着的排序,不做他用
map.put("ContactSortKey", sortKeyString.substring(0, 1).toUpperCase());
map.put("ContactName", name);
map.put("ContactTel", phoneNum);
map.put("ContactPhoto", R.drawable.adrlook);
list.add(map);//逐条记录插入
}
cursor.close();
// 对list进行排序
if (!list.isEmpty())
{
Collections.sort(list, new Comparator<Map<String, Object>>()
{
@Override
public int compare(Map<String, Object> object1,Map<String, Object> object2)
{
// 根据首字母文本排序
return ((String) object1.get("pinyin")).compareTo((String) object2.get("pinyin"));
}
});
}
}
return list;
}

public String toPinYin(String hanzis)// 获取汉语拼音
{
// System.out.println("转换前的中文为:" + hanzis);
String pinyinString = "";
char[] charArray = hanzis.trim().toCharArray();
// 根据需要定制输出格式,用默认的即可
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
try
{
// 遍历数组,ASC码大于128进行转换
for (int i = 0; i < charArray.length; i++)
{
if (charArray[i] > 128)
{
// System.out.println(charArray[i]+"->"+PinyinHelper.toHanyuPinyinStringArray(charArray[i],
// defaultFormat)+"=="+PinyinHelper.toHanyuPinyinStringArray(charArray[i],
// defaultFormat)[0]);
pinyinString += PinyinHelper.toHanyuPinyinStringArray(charArray[i], defaultFormat)[0];
} else
{
pinyinString += charArray[i];
}
}
} catch (BadHanyuPinyinOutputFormatCombination e)
{
e.printStackTrace();
//System.out.println(e.toString());
}
// System.out.println("转换后的拼音为:" + pinyinString);
return pinyinString.trim();
}
}
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号