0

我想在单击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 上键入名称,则项目将通过基本适配器出现,只有该项目应该可见或搜索

4

1 回答 1

1

在 onTextChangeListner 过滤器列表视图中取一个 EditText 和 ...

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

您的 ExchangeListner

@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) {


            }


        });
于 2013-05-21T09:30:06.397 回答