android中文首字母排序、分割

先上个图,   只实现了排序 以及加分割线, 那个提示,以及右边一个竖条字母定位没实现。 代码是借鉴别人的弄出来的

需要架包pinyin4j-2.5.0.jar

bug,是多音字没处理~

 android中文首字母排序、分割_第1张图片


PingYinUtil.java  获取拼音

public class PingYinUtil {
	 
	/**
	    * 
	    * @param inputString
	    * @return
	    */
	    public static String getPingYin(String inputString) {
	        HanyuPinyinOutputFormat format = new
	        HanyuPinyinOutputFormat();
	        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
	        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();
	        }
	        return output;
	    }
}

PinyinComparator.java 比较排序

/**
 * 只是判断了为""的时候,放到最前面,
 *  假如需要判断 符号,半角 全角,可以用正则表达式
 * 
 * @author zeng
 * 
 * 
 *         正则表达式 如下:
 *          只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。
   *           	只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。   
 *         	只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。
 *         	!!!-> 结束符号 $ 开始符号 ^
 */

public class PinyinComparator implements Comparator<TestSortItem> {

	@Override
	public int compare(TestSortItem o1, TestSortItem o2) {
		// TODO Auto-generated method stub
		String py1 = o1.getPinyin();
		String py2 = o2.getPinyin();
		// 判断是否为空""
		if (isEmpty(py1) && isEmpty(py2))
			return 0;
		if (isEmpty(py1))
			return -1;
		if (isEmpty(py2))
			return 1;
		String str1 = "";
		String str2 = "";
		try {
			str1 = ((o1.getPinyin()).toUpperCase()).substring(0, 1);
			str2 = ((o2.getPinyin()).toUpperCase()).substring(0, 1);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("某个str为\" \" 空");
		}
		return str1.compareTo(str2);
	}

	private boolean isEmpty(String str) {
		return "".equals(str.trim());
	}
}

 

obj

public class TestSortItem {

	private String name;
	private Object other = "test";
	private String pinyin;



ContactAdapter 

public class ContactAdapter extends MyBaseAdapter<TestSortItem> {

	public ContactAdapter(Context context, List<TestSortItem> list) {
		super(context, list);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = inflater.inflate(R.layout.friendlist_item_semis, null);
			vh.firstCharHint = (TextView) convertView.findViewById(R.id.text_first_char_hint);
			vh.name = (TextView) convertView.findViewById(R.id.name);
			vh.sign = (TextView) convertView.findViewById(R.id.sign);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		TestSortItem item = data.get(position);
		char previewChar = ' ';
		char currentChar = ' ';
		try {
			int idx = position - 1;
			previewChar = (idx >= 0 ? data.get(idx).getPinyin().charAt(0) : ' ');
			currentChar = item.getPinyin().charAt(0);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(" 防止某个为\"\" 空,报错");
		}
		if (currentChar != previewChar) {
			if (isLetter(item.getPinyin())) {
				vh.firstCharHint.setVisibility(View.VISIBLE);
				vh.firstCharHint.setText(String.valueOf(currentChar));
			} else {
				vh.firstCharHint.setVisibility(View.GONE);
			}
		} else {
			// 实例化一个CurrentView后,会被多次赋值并且只有最后一次赋值的position是正确
			vh.firstCharHint.setVisibility(View.GONE);
		}

		vh.name.setText(item.getName());
		vh.sign.setText(item.getOther().toString());
		return convertView;
	}

	private class ViewHolder {
		private TextView name;
		private TextView sign;
		private TextView firstCharHint;
	}

//	private boolean isNum(char c) {
//		if (java.lang.Character.isDigit(c)) {
//			return true;
//		}
//		return false;
//	}
//
//	private static boolean isLetter1111(String str) {
//		java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("[a-zA-Z]+");
//		java.util.regex.Matcher m = pattern.matcher(str);
//		return m.matches();
//	}

	/**
	 * 仅判断首字母
	 * 
	 * @param str
	 * @return
	 */
	private static boolean isLetter(String str) {
		java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("^[a-zA-Z]+");
		java.util.regex.Matcher m = pattern.matcher(str);
		return m.matches();
	}
}


MyBaseAdapter<T>  这个只是我为了 少写那几个经常重复重写的方法,getCount()...等。

public abstract class MyBaseAdapter<T> extends BaseAdapter {
	protected List<T> data;
	protected Context mContext;
	protected LayoutInflater inflater;

	public MyBaseAdapter(Context context, List<T> list) {
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.data = list;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	public void refreshList(List<T> list) {
		// for(int i = 0; i<list.size(); i++){
		// data.add(list.get(i));
		// }
		this.data = list;
		notifyDataSetChanged();
	}


activity

originalData = new ArrayList<TestSortItem>();
		initTestData();
		for (TestSortItem item : originalData) {
			item.setPinyin(PingYinUtil.getPingYin(item.getName().toString()));
			System.out.println("拼音 =>    " + item.getPinyin());
		}
		Collections.sort(originalData, new PinyinComparator());
		// 排序完后,看排序的list,
		for (TestSortItem item : originalData)
			System.out.println("排序==>   " + item.getName().toString());
		//
		adapter = new ContactAdapter(this, originalData);
		mListView.setAdapter(adapter);



 


你可能感兴趣的:(android中文首字母排序、分割)