1

SimpleAdapter でカスタム フィルターを動作させようとしていますが、最後のハードルに落ちているようです。コードをデバッグすると、publishResults メソッドの itemsFiltered で結果のフィルタリングされた ArrayList を確認できますが、完全なリストが常に表示されます。

フィルタリングされていない完全なリストではなく、フィルタリングされた結果リストをアダプタで動作させるにはどうすればよいですか?

コードは次のとおりです。

private class TextCharFilter extends Filter{

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        // convert search string to lower case - the filtering is not case sensitive
        constraint = constraint.toString().toLowerCase();

        // define the result object
        FilterResults result = new FilterResults();
        // define a place to hole the items that pass filtering         
        List<HashMap<String, String>> filteredItems = new ArrayList<HashMap<String,String>>();

        // loop through the original list and any items that pass filtering are added to the "filtered" list
        if(constraint != null && constraint.toString().length() > 0) {

            for(int i = 0; i < items.size(); i++) {
                HashMap<String, String> tmp = items.get(i);
                String candidate = tmp.get("PT").toLowerCase();

                if(candidate.contains(constraint) ) {
                    filteredItems.add(tmp);
                }
            }

            // set the result to the "filtered" list.
            result.count = filteredItems.size();
            result.values = filteredItems;

        }    
        else
        {
            // if nothing to filter on -  then the result is the complete input set
            synchronized(this)
            {
             result.values = items;
             result.count = items.size();
            }
        }
        return result;
    }

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

        ArrayList<HashMap<String, String>> tmp = (ArrayList<HashMap<String, String>>)results.values;

        itemsFiltered = new ArrayList<HashMap<String,String>>();

        for (int i = 0; i < tmp.size(); i++){
            itemsFiltered.add(tmp.get(i));
        }

        notifyDataSetChanged();

        notifyDataSetInvalidated();
    }

}
4

2 に答える 2

0

私も同じ問題を抱えていました。

このコードは私にとってはうまくいきます。

protected void publishResults(CharSequence constraint, FilterResults results) {
        arrayList.clear();
        arrayList.addAll((Collection<? extends HashMap<String, String>>) results.values);
        if (results.count > 0) {
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();
        }       
}
于 2014-01-05T18:33:38.797 に答える
0

publishResults() で作成された arraylist に項目を追加しているようです。項目が実際のアダプターに追加されることはありません。publishResults() でアダプターをクリアしてから、アイテムを再度追加する必要があります。または、フィルター リストから新しいアダプターを作成し、それをリストビューのアダプターとして設定するだけです。

于 2012-10-07T01:10:35.780 に答える