将中文转成拼音后排序

应用场景:在读取通讯录信息的时候需要安排名字排序(数字、字母(不区分大小写))。
注意:本例中使用了pinyin4j-2.5.0.jar这个包
主要类:
PinyinUtils.java
package hhf.sort;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
/**
* 拼音帮助类 公用的 大家可以随便拿着用调用里面的方法即可
* @author hhf
*
*/
public class PinyinUtils {
// 获得汉语拼音首字母
public static String getAlpha(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
pinyinName += PinyinHelper.toHanyuPinyinStringArray(
nameChar[i], defaultFormat)[0].charAt(0);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
return pinyinName;
}

/**
* 将字符串中的中文转化为拼音,其他字符不变
*
* @param inputString
* @return
*/
public static String getPingYin(String inputString) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
// format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);


char[] input = inputString.trim().toCharArray();
String output = "";

try {
for (int i = 0; i < input.length; i++) {
if (java.lang.Character.toString(input[i]).matches(
"[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(
input[i], format);
output += temp[0];
} else
output += java.lang.Character.toString(input[i]);
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
// System.out.println("getPingYin output = " + output);
return output;
}

/**
* 汉字转换位汉语拼音首字母,英文字符不变
*
* @param chines
* 汉字
* @return 拼音
*/
public static String converterToFirstSpell(String chines) {
String pinyinName = "";
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
pinyinName += PinyinHelper.toHanyuPinyinStringArray(
nameChar[i], defaultFormat)[0].charAt(0);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pinyinName += nameChar[i];
}
}
System.out.println("converterToFirstSpell pinyinName = " + pinyinName);
return pinyinName;
}
}


PinyinComparator.java

package hhf.sort;

import java.util.Comparator;


public class PinyinComparator implements Comparator{
@Override
public int compare(Object o1, Object o2) {
String str1 = PinyinUtils.getPingYin(((UserInfo) o1).getPy());
String str2 = PinyinUtils.getPingYin(((UserInfo) o2).getPy());
return str1.compareToIgnoreCase(str2);//不区分大小写
// return str1.compareTo(str2);//区分大小写
}

}

UserInfo.java

package hhf.sort;

public class UserInfo {
private String userName;
private String phoneNum;
private String py;

public UserInfo(String userName, String phoneNum,String py) {
super();
this.userName = userName;
this.phoneNum = phoneNum;

this.py=py;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getPy() {
return py;
}
public void setPy(String py) {
this.py = py;
}

public String toString() {
return "UserInfo [userName=" + userName + ", phoneNum=" + phoneNum
+ ", py=" + py + "]";
}
}



MyUserInfoAdapter.java

package hhf.sort;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyUserInfoAdapter extends BaseAdapter {

private Context context;

private List<UserInfo> userInfos;

public MyUserInfoAdapter(Context context, List<UserInfo> userInfos) {

this.context = context;
this.userInfos = userInfos;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return userInfos.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

// View view = convertView;
View view = convertView;

ViewHolder holder = null;
if (view == null) {

view = LayoutInflater.from(context).inflate(R.layout.list_item,
null);

holder = new ViewHolder();
view.setTag(holder);
holder.tv_catalog = (TextView) view.findViewById(R.id.tv);

} else {
holder = (ViewHolder) convertView.getTag();
}



String catalog = PinyinUtils.converterToFirstSpell(
userInfos.get(position).getUserName()).substring(0, 1);
System.out.println("catalog = " +catalog);
// if (position == 0) {
// holder.tv_catalog.setVisibility(View.VISIBLE);
// holder.tv_catalog.setText(catalog);
// } else {
// String lastCatalog = PinyinUtils.converterToFirstSpell(
// userInfos.get(position - 1).getUserName()).substring(0,
// 1);
// if (catalog.equals(lastCatalog)) {
// holder.tv_catalog.setVisibility(View.GONE);
// } else {
// holder.tv_catalog.setVisibility(View.VISIBLE);
// holder.tv_catalog.setText(catalog);
// }
// }
holder.tv_catalog.setText(userInfos.get(position).getUserName());
// holder.tv_nick.setText(userInfos.get(position).getUserName());
// holder.tv_mobile.setText(userInfos.get(position).getPhoneNum());

return view;
}

static class ViewHolder {

TextView tv_catalog;
TextView tv_nick;
TextView tv_mobile;
}

}


SortNameDemoActivity.java

package hhf.sort;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;


import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class SortNameDemoActivity extends Activity {
/** Called when the activity is first created. */
//ListView的数据集
private static List<UserInfo> userInfos;
private ListView mListView;
private MyUserInfoAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView) findViewById(R.id.listview);

getUserInfos();
adapter = new MyUserInfoAdapter(SortNameDemoActivity.this, userInfos);
mListView.setAdapter(adapter);
}

/**
* 初始化加载listVIew所需要的数据 并进行排序和匹配
*/
private void getUserInfos() {

UserInfo[] userinfoArray = new UserInfo[] {
new UserInfo("唐僧", "18765432345", PinyinUtils.getAlpha("唐僧")),
new UserInfo("猪师弟", "18765432345", PinyinUtils.getAlpha("猪师弟")),
new UserInfo("阿呆", "18765432345", PinyinUtils.getAlpha("阿呆")),
new UserInfo("8899", "18765432345",PinyinUtils.getAlpha("8899")),
new UserInfo("1899", "18765432345",PinyinUtils.getAlpha("1899")),
new UserInfo("孙悟空", "18765432345", PinyinUtils.getAlpha("孙悟空")),
new UserInfo("阿三", "18765432345", PinyinUtils.getAlpha("阿三")),
new UserInfo("张三", "18765432345", PinyinUtils.getAlpha("张三")),
new UserInfo("张二B", "18876569008", PinyinUtils.getAlpha("张二B")),
new UserInfo("阿三", "18765432345", PinyinUtils.getAlpha("阿三")),
new UserInfo("张三", "18765432345", PinyinUtils.getAlpha("张三")),
new UserInfo("张二B", "18876569008", PinyinUtils.getAlpha("张二B")),
new UserInfo("阿三", "18765432345", PinyinUtils.getAlpha("阿三")),
new UserInfo("张三", "18765432345", PinyinUtils.getAlpha("张三")),
new UserInfo("张二B", "18876569008", PinyinUtils.getAlpha("张二B")),
new UserInfo("阿三", "18765432345", PinyinUtils.getAlpha("阿三")),
new UserInfo("张三", "18765432345", PinyinUtils.getAlpha("张三")),
new UserInfo("张二B", "18876569008", PinyinUtils.getAlpha("张二B")),
new UserInfo("李四", "18909876545", PinyinUtils.getAlpha("李四")),
new UserInfo("王小二", "18909876545", PinyinUtils.getAlpha("王小二")),
new UserInfo("张三丰", "18909876545", PinyinUtils.getAlpha("张三丰")),
new UserInfo("郭靖", "18909876545", PinyinUtils.getAlpha("郭靖")),
new UserInfo("张无忌", "18909876545", PinyinUtils.getAlpha("张无忌")),
new UserInfo("a郭靖", "18909876545", PinyinUtils.getAlpha("a郭靖")),
new UserInfo("B张无忌", "18909876545", PinyinUtils.getAlpha("B张无忌")),
new UserInfo("b张无忌", "18909876545", PinyinUtils.getAlpha("b张无忌")),
new UserInfo("黄小贤", "18909876545", PinyinUtils.getAlpha("黄小贤")) };

Arrays.sort(userinfoArray, new PinyinComparator());
//集合的话用
//Collections.sort(list);
userInfos = Arrays.asList(userinfoArray);

}


}

相应的xml文件:
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listview" />

</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
>

<TextView
android:id="@+id/tv"
android:layout_height="wrap_content"
android:paddingRight="4.0dip"
android:layout_width="fill_parent"
android:background="#00ff00"/>

</LinearLayout>

运行效果如图:
将中文转成拼音后排序 - 无尘 - 无尘
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值