如何通过Base Adapter在Listview中按字母顺序搜索项目

时间:2013-05-21 09:14:09

标签: android listview baseadapter

我想在点击edittext上按字母顺序搜索列表视图中的项目我没有得到如何做到这一点

serch_item.addTextChangedListener(new TextWatcher() {
                @Override


                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub
                     text = serch_item.getText().toString();
                    //  approvedfriendList.setFilterText(text);
                }
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub
                }
                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub

                }
            });

search_item是editext,如果用户在edittext上键入名称,该项目应该可见或搜索,则项目将通过基本适配器传递

1 个答案:

答案 0 :(得分:1)

选择一个EditText&在onTextChangeListner过滤器listview ...

private class ModelFilter extends Filter
        {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                constraint = constraint.toString().toLowerCase();
                System.out.println("In performFiltering()");
                FilterResults result = new FilterResults();
                if(constraint != null && constraint.toString().length() > 0)
                {
                    ArrayList<Model> filteredItems = new ArrayList<Model>();

                    for(int i = 0, l = allModelItemsArray.size(); i < l; i++)
                    {
                        Model m = allModelItemsArray.get(i);
                        if(m.getWebsite().toLowerCase().contains(constraint))
                            filteredItems.add(m);
                    }
                    result.count = filteredItems.size();
                    result.values = filteredItems;
                }
                else
                {
                    synchronized(this)
                    {
                        result.values = allModelItemsArray;
                        result.count = allModelItemsArray.size();
                    }
                }
                return result;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                filteredModelItemsArray = (ArrayList<Model>)results.values;
                System.out.println("In publishResults()");
                notifyDataSetChanged();
                clear();
                for(int i = 0, l = filteredModelItemsArray.size(); i < l; i++)
                    add(filteredModelItemsArray.get(i));
                //notifyDataSetInvalidated();
            }
       }

您的TexchangeListner

@Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                BookmarkManagerActivity.this.adapter.getFilter().filter(s);
                adapter.notifyDataSetChanged();
            }



            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }



            @Override
            public void afterTextChanged(Editable s) {


            }


        });