SimpleCursorAdapterを元に作ったListViewにfilterを付加する
以下のように実装しているとする。
mListView.setTextFilterEnabled(true); mAdapter = new SimpleCursorAdapter( getActivity().getApplicationContext(), R.layout.list, null, new String[]{Helper.FACE, Helper.TAG}, new int[]{R.id.list_face, R.id.list_tag} ); mListView.setListAdapter(mAdapter);
以下のようにして表示されているリストをフィルタリングする。
public void onTextChanged(CharSequence s, int start, int before, int count) { mListView.setFilterText(s.toString); }
これだけでは上手く動作しない。
■解決策
adapterをListViewにセットする前に以下のように記述する必要がある。
mAdapter.setFilterQueryProvider(new FilterQueryProvider() { @Override public Cursor runQuery(CharSequence constraint) { String word = "%" + constraint.toString() + "%"; Cursor cursor = getActivity().managedQuery( Provider.URI, new String[]{Helper.ID, Helper.FACE, Helper.TAG, Helper.CREATED}, "`" + Helper.FACE + "` LIKE ? OR `" + Helper.TAG + "` LIKE ?", new String[]{word, word}, Helper.CREATED + " DESC" ); return cursor; } });
TrackBack URL :
Comments (0)
コメントはまだありません»
No comments yet.
RSS feed for comments on this post.TrackBack URL
Leave a comment