仿Android联系人SideBar排序,根据拼音A-Z字母快速导航联系人姓名( 二 )

parent, View view,int position, long id) {// 这里要利用adapter.getItem(position)来获取当前position所对应的对象// Toast.makeText(getApplication(),// ((SortModel)adapter.getItem(position)).getName(),// Toast.LENGTH_SHORT).show();String number = callRecords.get(((SortModel) adapter.getItem(position)).getName());Toast.makeText(MainActivity.this, number, 0).show();}});new ConstactAsyncTask().execute(0);}private class ConstactAsyncTask extendsAsyncTask {@Overrideprotected Integer doInBackground(Integer... arg0) {int result = -1;callRecords = ConstactUtil.getAllCallRecords(MainActivity.this);result = 1;return result;}@Overrideprotected void onPostExecute(Integer result) {super.onPostExecute(result);if (result == 1) {List constact = new ArrayList();for (Iterator keys = callRecords.keySet().iterator(); keys.hasNext();) {String key = keys.next();constact.add(key);}String[] names = new String[] {};names = constact.toArray(names);SourceDateList = filledData(names);// 根据a-z进行排序源数据Collections.sort(SourceDateList, pinyinComparator);adapter = new SortAdapter(MainActivity.this, SourceDateList);sortListView.setAdapter(adapter);mClearEditText = (ClearEditText) MainActivity.this.findViewById(R.id.filter_edit);mClearEditText.setOnFocusChangeListener(new OnFocusChangeListener() {@Overridepublic void onFocusChange(View arg0, boolean arg1) {mClearEditText.setGravity(Gravity.LEFT| Gravity.CENTER_VERTICAL);}});// 根据输入框输入值的改变来过滤搜索mClearEditText.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start,int before, int count) {// 当输入框里面的值为空 , 更新为原来的列表 , 否则为过滤数据列表filterData(s.toString());}@Overridepublic void beforeTextChanged(CharSequence s, int start,int count, int after) {}@Overridepublic void afterTextChanged(Editable s) {}});}}@Overrideprotected void onPreExecute() {super.onPreExecute();}}/*** 为ListView填充数据* * @param date* @return*/private List filledData(String[] date) {List mSortList = new ArrayList();for (int i = 0; i < date.length; i++) {SortModel sortModel = new SortModel();sortModel.setName(date[i]);// 汉字转换成拼音String pinyin = characterParser.getSelling(date[i]);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;} else {filterDateList.clear();for (SortModel 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);}}
5.为了获取到联系人的数据 , 我们首先当然是不要忘了添加获取联系人的权限了 , 其次这里有一个获取联系人的工具类 , 十分的方便:
public class ConstactUtil {/*** 获取所有数据* * @return*/public static Map getAllCallRecords(Context context) {Map temp = new HashMap();Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,ContactsContract.Contacts.DISPLAY_NAME+ " COLLATE LOCALIZED ASC");if (c.moveToFirst()) {do {// 获得联系人的ID号String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));// 获得联系人姓名String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));// 查看该联系人有多少个电话号码 。如果没有这返回值为0int phoneCount = c.getInt(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));String number = null;if (phoneCount > 0) {// 获得联系人的电话号码Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + contactId, null, null);if (phones.moveToFirst()) {number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));}phones.close();}temp.put(name, number);} while (c.moveToNext());}c.close();return temp;}}