贴一下关键代码
查找姓名部分:
mSearchTextCtrl = (EditText) findViewById(R.id.edit_searchText);
mSearchTextCtrl.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String content = mSearchTextCtrl.getText().toString();
if (content.length() > 0) {
m_PBSortAdapter.updateListView(search(content));
} else {
m_PBSortAdapter.updateListView(mAllContactsList);
}
mPhonebookListCtrl.setSelection(0);
}
});
changeSearchHintText();
private List<SortModel> search(String str) {
List<SortModel> filterList = new ArrayList<SortModel>();
// if (str.matches("^([0-9]|[/+])*$")) {
if (str.matches("^([0-9]|[/+]).*")) {
String simpleStr = str.replaceAll("\\-|\\s", "");
for (SortModel contact : mAllContactsList) {
if (contact.number != null && contact.name != null) {
if (contact.simpleNumber.contains(simpleStr) || contact.name.contains(str)) {
if (!filterList.contains(contact)) {
filterList.add(contact);
}
}
}
}
} else {
for (SortModel contact : mAllContactsList) {
if (contact.number != null && contact.name != null) {
if (contact.name.toLowerCase(Locale.CHINESE).contains(str.toLowerCase(Locale.CHINESE))
|| contact.sortKey.toLowerCase(Locale.CHINESE).replace(" ", "")
.contains(str.toLowerCase(Locale.CHINESE))
|| contact.sortToken.simpleSpell.toLowerCase(Locale.CHINESE)
.contains(str.toLowerCase(Locale.CHINESE))
|| contact.sortToken.wholeSpell.toLowerCase(Locale.CHINESE)
.contains(str.toLowerCase(Locale.CHINESE))) {
if (!filterList.contains(contact)) {
filterList.add(contact);
}
}
}
}
}
return filterList;
}
首字母排序ContactsSortAdapter.class:
public View getView(final int position, View view, ViewGroup parent) {
ViewHolder viewHolder = null;
final SortModel mContent = mList.get(position);
if (view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(mContext).inflate(R.layout.phonebook_item, parent, false);
viewHolder.tvTitle = (TextView) view.findViewById(R.id.pb_name);
viewHolder.tvNumber = (TextView) view.findViewById(R.id.pb_phone);
viewHolder.tvLetter = (TextView) view.findViewById(R.id.pb_catalog);
viewHolder.ivImage = (ImageView) view.findViewById(R.id.pb_image);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
int section = getSectionForPosition(position);
if (position == getPositionForSection(section)) {
viewHolder.tvLetter.setVisibility(View.VISIBLE);
viewHolder.tvLetter.setText(mContent.sortLetters);
} else {
viewHolder.tvLetter.setVisibility(View.GONE);
}
viewHolder.ivImage.setImageDrawable(dUserIcon);
viewHolder.tvTitle.setText(this.mList.get(position).name);
viewHolder.tvNumber.setText(this.mList.get(position).number);
return view;
}
public static class ViewHolder {
public TextView tvLetter = null;
public TextView tvTitle = null;
public TextView tvNumber = null;
public ImageView ivImage = null;
}
public int getSectionForPosition(int position) {
return mList.get(position).sortLetters.charAt(0);
}
public int getPositionForSection(int section) {
for (int i = 0; i < getCount(); i++) {
String sortStr = mList.get(i).sortLetters;
char firstChar = sortStr.toUpperCase(Locale.CHINESE).charAt(0);
if (firstChar == section) {
return i;
}
}
return -1;
}
贴图: