在listview中获取搜索数据的问题

时间:2017-04-09 07:40:19

标签: java android listview android-package-managers android-search

我已经在customadapter类中实现了filterable,我可以搜索listview,但是当我清空我的搜索框时,我的列表仍然显示最后一个搜索应用程序而不是整个应用程序列表。

mainactivity.java

 public class MainActivity extends ListActivity {
 List<ApplicationInfo> appList;
 ListAdapter listAdapter;
 PackageManager packageManager;
 Process suProcess;
 DataOutputStream os;
 ApplicationInfo info;
 EditText search;
 ArrayList<ApplicationInfo> applist;

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

    packageManager = getPackageManager();
    new LoadApplication().execute();

    search = (EditText) findViewById(R.id.searchView);
    search.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            listAdapter.getFilter().filter(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
 }
 }

我添加了我的过滤类。

private class ListFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if (constraint == null || constraint.length() == 0) {
            results.values = applist;
            results.count = applist.size();
        } else {
            ArrayList<ApplicationInfo> info = new ArrayList<ApplicationInfo>();

            for (ApplicationInfo c : applist) {
                if (c.loadLabel(packageManager).toString().toUpperCase().contains(constraint.toString().toUpperCase())) {
                    info.add(c);
                }
            }
            results.values = info;
            results.count = info.size();
        }
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        if (results.count == 0) {
            notifyDataSetInvalidated();
        } else {
            applist = (List<ApplicationInfo>) results.values;
            notifyDataSetChanged();
        }
    }
}

并覆盖了getFilter()方法。

 public Filter getFilter() {

    if (filter == null)
    {
        filter=new ListFilter();
    }
    return filter;
}

1 个答案:

答案 0 :(得分:1)

对于添加搜索,您需要添加过滤器,如下面的代码:

 private class ItemFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final List<String> list = originalData;

            int count = list.size();
            final ArrayList<String> nlist = new ArrayList<String>(count);

            String filterableString ;

            for (int i = 0; i < count; i++) {
                filterableString = list.get(i);
                if (filterableString.toLowerCase().contains(filterString)) {
                    nlist.add(filterableString);
                }
            }

            results.values = nlist;
            results.count = nlist.size();

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredData = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }

    }

按照以下链接并在适配器中添加过滤器:

Filtering ListView with custom (object) adapter