工具栏中的SearchView无法过滤ListFragment

时间:2018-11-21 22:59:43

标签: java android android-listview android-adapter android-listfragment

我正在尝试在SearchView的{​​{1}}内实现Toolbar,但是由于某些原因,过滤无法正常工作。 ListFragment和键盘看起来正常,但是在我输入内容时SearchView并没有被过滤。

片段类

ListView

适配器类

public class MyFragment extends ListFragment implements SearchView.OnQueryTextListener {

    public MyFragment() {}

    private ListView lv;
    private MyListAdapter mAdapter;

    public static MyFragment newInstance() {
        return new MyFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        initialize();
        return view;
    }

    List<Product> myList = new ArrayList<>();
    private void initialize() {
        String[] items = getActivity().getResources().getStringArray(R.array.product_names);
        String[] itemDescriptions = getActivity().getResources().getStringArray(R.array.product_descriptions);
        for (int n = 0; n < items.length; n++){
            Product product = new Product();
            product.setProductName(items[n]);
            product.setProductDescription(itemDescriptions[n]);
            myList.add(product);
        }

        mAdapter = new MyListAdapter(myList, getActivity());
        setListAdapter(mAdapter);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        View v = getView();

        mTwoPane = getActivity().findViewById(R.id.detail_container) != null;

        assert v != null;
        lv = v.findViewById(android.R.id.list);
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MyListAdapter adapter = (MyListAdapter) parent.getAdapter();
            }
        });

        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        MenuInflater mInflater = Objects.requireNonNull(getActivity()).getMenuInflater();
        mInflater.inflate(R.menu.menu_search, menu);

        MenuItem searchitem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) searchitem.getActionView();
        searchView.setQueryHint(Objects.requireNonNull(getContext()).getText(R.string.searchhint_productname));

        super.onCreateOptionsMenu(menu, inflater);
    }
}

1 个答案:

答案 0 :(得分:1)

您在OnQueryTextListener上缺少SearchView,只需附加一个,以便适配器知道何时进行过滤:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override public boolean onQueryTextChange(String newText) {
        mAdapter.getFilter().filter(newText);
        return true;
    }
});