博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 联系人列表功能及排序问题解决
阅读量:5823 次
发布时间:2019-06-18

本文共 8911 字,大约阅读时间需要 29 分钟。

网上的这种demo太多太多了,但是他们只能实现基本的A-Z排序,也就是仅仅是首字母排序,涉及到一些特别人名字排序的时候相同姓氏会被分开,这也是由于他是按照把中文汉字全部转化为汉语拼音后进行一一比较而造成的结果,给一张图就很明了了

是不是发现姓李中间出现了姓刘的情况?这是因为 李四光的拼音是lisiguang 而刘德华呢是liudehua,李小龙是lixiaolong,

这样应该很清楚了吧,这种排序显然达不到我们实际的效果

我在这里帖上关键的代码吧,其他的网上也都 有就不多BB了

import java.text.Collator;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.text.Editable;import android.text.TextUtils;import android.text.TextWatcher;import android.view.View;import android.view.ViewGroup.MarginLayoutParams;import android.widget.AbsListView;import android.widget.AbsListView.OnScrollListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.SectionIndexer;import android.widget.TextView;import android.widget.Toast;import com.example.sortlistview.SideBar.OnTouchingLetterChangedListener;public class ActivityAddFriends extends Activity implements SectionIndexer {	private ListView sortListView;	private SideBar sideBar;	private TextView dialog;	private SortGroupMemberAdapter adapter;	private ClearEditText mClearEditText;	private LinearLayout titleLayout;	private TextView title;	private TextView tvNofriends;	/**	 * 上次第一个可见元素,用于滚动时记录标识。	 */	private int lastFirstVisibleItem = -1;	/**	 * 汉字转换成拼音的类	 */	private CharacterParser characterParser;	private List
SourceDateList; private List
SourceDateListTwo = new ArrayList
(); /** * 根据拼音来排列ListView里面的数据类 */ private PinyinComparator pinyinComparator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_friends); initViews(); // System.out.println("qqqqqqq==");// String[] newArray = getResources().getStringArray(R.array.date) ; // Comparator
com = Collator.getInstance(java.util.Locale.CHINA); // List
list = Arrays.asList(newArray); // Collections.sort(list, com); // System.out.println("qqqqqqq=="+list); // for(String i:list){ // System.out.println("qqqqqqq=="+i); // } // } private void initViews() { titleLayout = (LinearLayout) findViewById(R.id.title_layout); title = (TextView) this.findViewById(R.id.title_layout_catalog); tvNofriends = (TextView) this .findViewById(R.id.title_layout_no_friends); // 实例化汉字转拼音类 characterParser = CharacterParser.getInstance(); pinyinComparator = new PinyinComparator(); sideBar = (SideBar) findViewById(R.id.sidrbar); dialog = (TextView) findViewById(R.id.dialog); sideBar.setTextView(dialog); // 设置右侧触摸监听 sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() { @Override public void onTouchingLetterChanged(String s) { // 该字母首次出现的位置 int position = adapter.getPositionForSection(s.charAt(0)); if (position != -1) { sortListView.setSelection(position); } } }); sortListView = (ListView) findViewById(R.id.country_lvcountry); sortListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView
parent, View view, int position, long id) { // 这里要利用adapter.getItem(position)来获取当前position所对应的对象 Toast.makeText( getApplication(), ((GroupMemberBean) adapter.getItem(position)).getName(), Toast.LENGTH_SHORT).show(); } });// SourceDateList = filledData(getResources().getStringArray(R.array.date)); String[] newArray = getResources().getStringArray(R.array.date) ; Comparator
com = Collator.getInstance(java.util.Locale.CHINA); List
list = Arrays.asList(newArray); Collections.sort(list, com); // for(String i:list){ // } SourceDateListTwo = filledDataTwo(list) ; SourceDateList = filledData(SourceDateListTwo) ; System.out.println("qqqqqqq=="+SourceDateList.toString()); // // 根据a-z进行排序源数据 Collections.sort(SourceDateList, pinyinComparator); adapter = new SortGroupMemberAdapter(this, SourceDateList); sortListView.setAdapter(adapter); sortListView.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { int section = getSectionForPosition(firstVisibleItem); int nextSection = getSectionForPosition(firstVisibleItem + 1); int nextSecPosition = getPositionForSection(+nextSection); if (firstVisibleItem != lastFirstVisibleItem) { MarginLayoutParams params = (MarginLayoutParams) titleLayout .getLayoutParams(); params.topMargin = 0; titleLayout.setLayoutParams(params); title.setText(SourceDateList.get( getPositionForSection(section)).getSortLetters()); } if (nextSecPosition == firstVisibleItem + 1) { View childView = view.getChildAt(0); if (childView != null) { int titleHeight = titleLayout.getHeight(); int bottom = childView.getBottom(); MarginLayoutParams params = (MarginLayoutParams) titleLayout .getLayoutParams(); if (bottom < titleHeight) { float pushedDistance = bottom - titleHeight; params.topMargin = (int) pushedDistance; titleLayout.setLayoutParams(params); } else { if (params.topMargin != 0) { params.topMargin = 0; titleLayout.setLayoutParams(params); } } } } lastFirstVisibleItem = firstVisibleItem; } }); mClearEditText = (ClearEditText) findViewById(R.id.filter_edit); // 根据输入框输入值的改变来过滤搜索 mClearEditText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // 这个时候不需要挤压效果 就把他隐藏掉 titleLayout.setVisibility(View.GONE); // 当输入框里面的值为空,更新为原来的列表,否则为过滤数据列表 filterData(s.toString()); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }); } /** * 为ListView填充数据 * * @param date * @return */ private List
filledData(List
SourceDateListTwo) { List
mSortList = new ArrayList
(); for (int i = 0; i < SourceDateListTwo.size(); i++) { GroupMemberBean sortModel = new GroupMemberBean(); sortModel.setName(SourceDateListTwo.get(i).getName()); // 汉字转换成拼音 String pinyin = characterParser.getSelling(SourceDateListTwo.get(i).getName()); String sortString = pinyin.substring(0, 1).toUpperCase(); // 正则表达式,判断首字母是否是英文字母 if (sortString.matches("[A-Z]")) { sortModel.setSortLetters(sortString.toUpperCase()); } else { sortModel.setSortLetters("#"); } mSortList.add(sortModel); } return mSortList; } /** * 为ListView填充数据 * * @param date * @return */ private List
filledDataTwo(List
list) { List
mSortList = new ArrayList
(); for (int i = 0; i < list.size(); i++) { GroupMemberBean sortModel = new GroupMemberBean(); sortModel.setName(list.get(i)); // 汉字转换成拼音 String pinyin = characterParser.getSelling(list.get(i)); System.out.println("pinyin=="+pinyin); String sortString = pinyin.substring(0, 1).toUpperCase(); // 正则表达式,判断首字母是否是英文字母 if (sortString.matches("[A-Z]")) { sortModel.setSortLetters(sortString.toUpperCase()); } else { sortModel.setSortLetters("#"); } mSortList.add(sortModel); } return mSortList; } /** * 根据输入框中的值来过滤数据并更新ListView * * @param filterStr */ private void filterData(String filterStr) { List
filterDateList = new ArrayList
(); if (TextUtils.isEmpty(filterStr)) { filterDateList = SourceDateList; tvNofriends.setVisibility(View.GONE); } else { filterDateList.clear(); for (GroupMemberBean sortModel : SourceDateList) { String name = sortModel.getName(); if (name.indexOf(filterStr.toString()) != -1 || characterParser.getSelling(name).startsWith( filterStr.toString())) { filterDateList.add(sortModel); } } } // 根据a-z进行排序 Collections.sort(filterDateList, pinyinComparator); adapter.updateListView(filterDateList); if (filterDateList.size() == 0) { tvNofriends.setVisibility(View.VISIBLE); } } @Override public Object[] getSections() { return null; } /** * 根据ListView的当前位置获取分类的首字母的Char ascii值 */ public int getSectionForPosition(int position) { return SourceDateList.get(position).getSortLetters().charAt(0); } /** * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置 */ public int getPositionForSection(int section) { for (int i = 0; i < SourceDateList.size(); i++) { String sortStr = SourceDateList.get(i).getSortLetters(); char firstChar = sortStr.toUpperCase().charAt(0); if (firstChar == section) { return i; } } return -1; }}复制代码

这里面主要代码是这块:

String[] newArray = getResources().getStringArray(R.array.date) ;         Comparator com = Collator.getInstance(java.util.Locale.CHINA);            List
list = Arrays.asList(newArray); Collections.sort(list, com); // for(String i:list){ // } SourceDateListTwo = filledDataTwo(list) ; SourceDateList = filledData(SourceDateListTwo) ;复制代码

"date"就是数据,我这里是测试的死的数据 ,改成从服务器获取的数据就行了

先将中文进行排序再按首字母进行排序就行了。

转载地址:http://uhbdx.baihongyu.com/

你可能感兴趣的文章
RecyclerView预加载机制源码分析
查看>>
直播源码开发视频直播平台,不得不了解的流程
查看>>
Ubuntu上的pycrypto给出了编译器错误
查看>>
聊聊flink的RestClientConfiguration
查看>>
在CentOS上搭建git仓库服务器以及mac端进行克隆和提交到远程git仓库
查看>>
測試文章
查看>>
Flex很难?一文就足够了
查看>>
【BATJ面试必会】JAVA面试到底需要掌握什么?【上】
查看>>
微服务b2b b2c o2o电子商务云平台
查看>>
上手kubernetes之前,你应该知道这6件事
查看>>
CollabNet_Subversion小结
查看>>
mysql定时备份自动上传
查看>>
Windows Thin PC安装功能组件
查看>>
Linux 高可用集群解决方案
查看>>
[install-pear-installer] Error 127 安装PHP时错误
查看>>
17岁时少年决定把海洋洗干净,现在21岁的他做到了
查看>>
CBO中 SMON 进程与 col_usage$ 的维护
查看>>
linux 启动oracle
查看>>
TeamCity 持续集成在LINUX的安装
查看>>
LOGGING 、FORCE LOGGING 、NOLOGGING、归档模
查看>>