Android:适配器中的过滤无效

时间:2015-11-01 16:46:21

标签: android filtering baseadapter

我有以下类过滤onQueryTextChange上的被调用者。问题是找到的结果未在找到的结果列表中更新。例如,我有列表(见下图。):

enter image description here

我输入了查询" TEST 1",因此更新的结果应该只包含一个找到的行。

但结果列表仍然相同。

我想问一下我应该更新什么来以正确的方式获得过滤结果。

非常感谢您的任何建议。

MainActivity

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String s) {
                Logger.d("onQueryTextSubmit ");
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                Logger.d(s);
                Logger.d("onQueryTextChange ");
                mAdapter = new WlanNetworkAdapter(DataContainer.getAccessPointList(), getParent()).getFilter();
                mAdapter.filter(s.toString());
                //TODO: https://coderwall.com/p/zpwrsg/add-search-function-to-list-view-in-android
                return true;
            }
        });

适配器:

public class WlanNetworkAdapter extends BaseAdapter implements Filterable {

    private List<WlanNetwork> mAccessPointlist;
    private Activity context;
    private Filter mFilter;
    LayoutInflater inflater;
    private List<WlanNetwork> filteredWlanList;

    public WlanNetworkAdapter(List<WlanNetwork> mAccessPointlist, Activity context) {
        this.mAccessPointlist = mAccessPointlist;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mAccessPointlist.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mAccessPointlist.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int arg0, View rowView, ViewGroup arg2) {
        //With this simple check, we can save the overhead of inflating a new view for each row.
        //This will make our listview faster.
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.list_item_found_wlan_data, arg2, false);
        }

        // Set text values to textViews in the item
        TextView ssidTv = (TextView) rowView.findViewById(R.id.found_wlan_ssid_tv);
        ssidTv.setText(mAccessPointlist.get(arg0).getSsid());

        TextView passwordTv = (TextView) rowView.findViewById(R.id.found_wlan_password_tv);
        passwordTv.setText(mAccessPointlist.get(arg0).getPsk());

        return rowView;
    }

    @Override
    public Filter getFilter() {

        Filter filter = new Filter() {

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

                mAccessPointlist = (List<WlanNetwork>) results.values; // has the filtered values
                notifyDataSetChanged();  // notifies the data with new filtered values
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
                // We implement here the filter logic
                if (constraint == null || constraint.length() == 0) {
                    Logger.d("Search Query empty, display all values");
                    // No filter implemented we return all the list
                    results.values = FoundController.getAccessPointsList();
                    results.count = FoundController.getAccessPointsList().size();
                } else {
                    Logger.d("Search query is: " + constraint.toString());
                    // We perform filtering operation
                    filteredWlanList = new ArrayList<WlanNetwork>();

                    for (WlanNetwork wlanNetwork : FoundController.getAccessPointsList()) {
                        if (wlanNetwork.getSsid().toUpperCase().startsWith(constraint.toString().toUpperCase())) {
                            Logger.d("Found");
                            filteredWlanList.add(wlanNetwork);
                        }
                    }
                    Logger.d("Count is :");
                    Logger.d(String.valueOf(filteredWlanList.size()));
                    results.values = filteredWlanList;
                    results.count = filteredWlanList.size();
                }
                return results;
            }

        };
        return filter;
    }

}

1 个答案:

答案 0 :(得分:-1)

更改您的MainActivity代码:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String s) {
            Logger.d("onQueryTextSubmit ");
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            if (adapter != null)
                adapter.getFilter().filter(s);
            return false;
        }
    });