ListView首字母排序+右侧字母索引+首字母查找姓名

贴一下关键代码
查找姓名部分:

    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;
    }

贴图:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

源码点击下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值