我有一个字符串列表,每个字符串可能包含中文字符或数字或英文字符。 (例如:“z莉z莉”,“露西”,“蒋豪”,“qwer”,“zout1iao”,“hah”)。我想做的是按照Android的联系人应用程序按英文字母顺序排序。(即“hah”,“蒋豪”,“露西”,“qwer”,“z莉莉莉”,“zout1iao” )所以我的策略是将所有汉字转换成拼音,然后像往常一样比较它们。 我已经尝试过库yinpin4j,它在J2SE平台上运行良好,但是当我在Android上运行相同的代码时,它引发了一个错误。 java代码是:
@Override
public int compareTo(People another) {
String onePinyin = HanziHelper.words2Pinyin(name);
String theOtherPinyin = HanziHelper.words2Pinyin(another.getName());
return onePinyin.compareTo(theOtherPinyin);
}
private static String char2String(char c) {
StringBuilder sb = new StringBuilder();
return sb.append(c).toString();
}
public static String char2Pinyin(char c) {
String[] pinyin = null;
try {
pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format);
} catch(BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
if(pinyin == null) {
return char2String(c);
} else {
return pinyin[0];
}
}
public static String words2Pinyin(String words) {
StringBuilder sb = new StringBuilder();
char[] chars = words.toCharArray();
for(int i = 0, length = chars.length; i < length; i++) {
sb.append(char2Pinyin(chars[i]));
}
return sb.toString();
}
错误是:
04-15 12:37:15.750: W/System.err(6898): java.io.IOException: BufferedInputStream is closed
04-15 12:37:15.791: W/System.err(6898): at java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:116)
04-15 12:37:15.791: W/System.err(6898): at java.io.BufferedInputStream.read(BufferedInputStream.java:294)
04-15 12:37:15.791: W/System.err(6898): at java.io.InputStreamReader.read(InputStreamReader.java:255)
04-15 12:37:15.791: W/System.err(6898): at java.io.BufferedReader.fillBuf(BufferedReader.java:128)
04-15 12:37:15.791: W/System.err(6898): at java.io.BufferedReader.read(BufferedReader.java:236)
04-15 12:37:15.791: W/System.err(6898): at java.util.Properties.load(Properties.java:307)
04-15 12:37:15.791: W/System.err(6898): at java.util.Properties.load(Properties.java:266)
04-15 12:37:15.791: W/System.err(6898): at net.sourceforge.pinyin4j.ChineseToPinyinResource.initializeResource(Unknown Source)
04-15 12:37:15.791: W/System.err(6898): at net.sourceforge.pinyin4j.ChineseToPinyinResource.<init>(Unknown Source)
04-15 12:37:15.791: W/System.err(6898): at net.sourceforge.pinyin4j.ChineseToPinyinResource.<init>(Unknown Source)
04-15 12:37:15.791: W/System.err(6898): at net.sourceforge.pinyin4j.ChineseToPinyinResource$ChineseToPinyinResourceHolder.<clinit>(Unknown Source)
04-15 12:37:15.791: W/System.err(6898): at net.sourceforge.pinyin4j.ChineseToPinyinResource.getInstance(Unknown Source)
04-15 12:37:15.796: W/System.err(6898): at net.sourceforge.pinyin4j.PinyinHelper.getUnformattedHanyuPinyinStringArray(Unknown Source)
04-15 12:37:15.796: W/System.err(6898): at net.sourceforge.pinyin4j.PinyinHelper.getFormattedHanyuPinyinStringArray(Unknown Source)
04-15 12:37:15.796: W/System.err(6898): at net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(Unknown Source)
04-15 12:37:15.796: W/System.err(6898): at com.sf.tools.HanziHelper.char2Pinyin(HanziHelper.java:29)
04-15 12:37:15.796: W/System.err(6898): at com.sf.tools.HanziHelper.words2Pinyin(HanziHelper.java:44)
04-15 12:37:15.796: W/System.err(6898): at com.sf.parse.PeopleListParser$Result$People.compareTo(PeopleListParser.java:156)
04-15 12:37:15.796: W/System.err(6898): at com.sf.parse.PeopleListParser$Result$People.compareTo(PeopleListParser.java:1)
04-15 12:37:15.796: W/System.err(6898): at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:286)
04-15 12:37:15.796: W/System.err(6898): at java.util.ComparableTimSort.sort(ComparableTimSort.java:153)
04-15 12:37:15.796: W/System.err(6898): at java.util.ComparableTimSort.sort(ComparableTimSort.java:142)
04-15 12:37:15.796: W/System.err(6898): at java.util.Arrays.sort(Arrays.java:1974)
04-15 12:37:15.796: W/System.err(6898): at java.util.Collections.sort(Collections.java:1941)
04-15 12:37:15.796: W/System.err(6898): at com.sf.activity.PeopleListActivity.initPageView(PeopleListActivity.java:73)
04-15 12:37:15.796: W/System.err(6898): at com.sf.activity.ReceiverListActivity.initPageView(ReceiverListActivity.java:23)
04-15 12:37:15.796: W/System.err(6898): at com.yek.android.base.BaseActivity.onCreate(BaseActivity.java:158)
然后我改为使用Collator usCollator = Collator.getInstance(Locale.SIMPLIFIED_CHINESE); usCollator.setStrength(Collator.PRIMARY);
如果只有汉字,它运作良好。在上面的例子中,这个方法将分别对中文和英文进行排序。
那么,你有什么想法吗?
答案 0 :(得分:0)
通讯录应用使用“COLLATE LOCALIZED ASC”。你可以试试同样的事情
答案 1 :(得分:0)
我的建议是,通过codepoint解析“word”代码点并构建一个新字符串。如果下一个代码点是Latin / Common,请不要调用words2Pinyin(),只需将聊天添加到新构建的字符串即可。它是中文,调用words2Pinyin()并将拼音添加到新字符串中。环。最后,你有一个干净的拉丁字符串。
答案 2 :(得分:0)
你可以谷歌一个jar似乎是pinyin.jar,它可以帮到你
答案 3 :(得分:0)
如果您要从手机中提取联系人数据(例如姓名,电话号码,电子邮件......),请尝试再拉一列ContactsContract.Contacts.SORT_KEY_PRIMARY
,它会将显示名称的拼音与显示名称一起存储(如果有的话) )。
让我们说DISPLAY_NAME
“阿妹”,其SORT_KEY_PRIMARY
将是“阿美妹”; “大姨”,排序键是“DA大YI姨”。从那里,您可以轻松应用字母排序并映射到原始显示名称。 :)