FilterListView - 从列表中删除不以提供的前缀开头的项目

Posted

技术标签:

【中文标题】FilterListView - 从列表中删除不以提供的前缀开头的项目【英文标题】:FilterListView - Remove items from list that don't begin with supplied prefix 【发布时间】:2015-09-10 21:13:16 【问题描述】:

我创建了一个运行良好的过滤列表视图。但是我只是想知道,当我在搜索视图中键入内容时,如何让我的列表视图删除不以提供的前缀开头的项目?如提供的屏幕截图所示,当我输入字母“p”时,我的所有项目都没有以该字母开头,但列表并没有消失。

public class ItemListAdapter extends BaseAdapter implements Filterable 

    private List<Victoria> mData;
    private List<Victoria> mFilteredData;
    private LayoutInflater mInflater;
    private ItemFilter mFilter;

    public ItemListAdapter (List<Victoria> data, Context context) 
        mData = data;
        mFilteredData = new ArrayList(mData);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    

    @Override
    public int getCount() 
        return mFilteredData.size();
    

    @Override
    public String getItem(int position) 
        return mFilteredData.get(position).getItem();
    

    @Override
    public long getItemId(int position) 
        return position;
    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 

        ViewHolder holder;
        if (convertView == null) 
           convertView = mInflater.inflate(R.layout.item_row, parent, false);
            holder = new ViewHolder();

            holder.title = (TextView) convertView.findViewById(R.id.item_title);
            holder.description = (TextView) convertView.findViewById(R.id.item_description);

            convertView.setTag(holder);
         else 
            holder = (ViewHolder) convertView.getTag();
        

        holder.title.setText(mFilteredData.get(position).getItem());
        holder.description.setText(mFilteredData.get(position).getItemDescription());

        return convertView;
    

    @Override
    public Filter getFilter() 
        if (mFilter == null) 
            mFilter = new ItemFilter();
        
        return mFilter;
    

    static class ViewHolder 
        private TextView title;
        private TextView description;
    

    private class ItemFilter extends Filter 
        @Override
        protected FilterResults performFiltering(CharSequence constraint) 
            FilterResults results = new FilterResults();

            if (TextUtils.isEmpty(constraint)) 
                results.count = mData.size();
                results.values = new ArrayList(mData);
             else 
                List<Victoria> resultList = new ArrayList<Victoria>();
                for (Victoria str : mData) 
                    if (str.getItemDescription().toLowerCase().contains(constraint.toString().toLowerCase())) 
                        resultList.add(str);
                    
                
                results.count = resultList.size();
                results.values = resultList;
            
            return results;
        

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) 
            if (results.count == 0) 
                mFilteredData.clear();
                notifyDataSetInvalidated();
             else 
                mFilteredData = (ArrayList<Victoria>)results.values;
                notifyDataSetChanged();
            
        
    

【问题讨论】:

看来您要么需要重做 performFiltering 方法才能使用 String.startsWith(prefix),要么创建新的过滤方法。 您的商品的所有描述都包含字母 p(欧洲描述ption 等),并且您正在使用contains 进行过滤。请参阅if (str.getItemDescription().toLowerCase().contains(constraint.toString().toLowerCase())) 行。应该像帕特里克所说的那样。 傻我,那应该是if (str.getItem()...问题解决了 【参考方案1】:

根据android Adding Search Functionality to ListView, 您可以使用如下代码在 ListView 中进行搜索:

MainActivity.java
public class MainActivity extends Activity 

// List view
private ListView lv;

// Listview Adapter
ArrayAdapter<String> adapter;

// Search EditText
EditText inputSearch;


// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;

@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Listview Data
    String products[] = "Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                            "iPhone 4S", "Samsung Galaxy Note 800",
                            "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro";

    lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
    lv.setAdapter(adapter);

    /**
     * Enabling Search Filter
     * */
    inputSearch.addTextChangedListener(new TextWatcher() 

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) 
            // When user changed the Text
            MainActivity.this.adapter.getFilter().filter(cs);   
        

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) 
            // TODO Auto-generated method stub

        

        @Override
        public void afterTextChanged(Editable arg0) 
            // TODO Auto-generated method stub                          
        
    );
    

【讨论】:

以上是关于FilterListView - 从列表中删除不以提供的前缀开头的项目的主要内容,如果未能解决你的问题,请参考以下文章

删除CSV文件中不以python中的数字开头的所有行

为啥数组的索引/列表不以 1 开头? [复制]

获取城市名称不以元音开头或不以元音结尾

正则表达式:如何匹配不以前缀列表开头的子字符串

.net 5 MVC 多选列表不以多部分形式返回值

如何删除所有不以某些字符开头的行?