可扩展列表视图实现可过滤

时间:2015-05-10 07:52:59

标签: android expandablelistview baseadapter searchview android-filterable

我在使用ValueFilter class时遇到问题,我的列表工作正常,但我的过滤器无效,并在NullPointerException返回PublishResults,请参阅我的代码:

  

BaseExpandableListAdapter.java

public class myExpandableListView extends BaseExpandableListAdapter implements Filterable {
        private Context context;
        private HashMap<String, List<String>> categories;
        private List<String> categoryList;

        // store temp data for filter
        private HashMap<String, List<String>> filter;
        ValueFilter valueFilter;

        public ExpandableListAdapter(
                 Context m_context, 
                 HashMap<String, List<String>> m_categories, 
                 List<String> m_categoryList) {
                 super();
                 this.context = m_context; 
                 this.categories = m_categories;
                 this.categoryList = m_categoryList;
                 this.filter = m_categories;
       }
       ...

    @Override
    public void notifyDataSetChanged() {
       super.notifyDataSetChanged();
    }

    public void notifyDataSetInvalidated(){
        super.notifyDataSetInvalidated();
    }

    @Override
    public Filter getFilter() {
        if(valueFilter == null)
            valueFilter = new ValueFilter();
        return valueFilter;
    }

    private class ValueFilter extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            constraint = searchView.getQuery().toString().toLowerCase();
            FilterResults results = new FilterResults();
            if(constraint != null && constraint.toString().length() > 0){
                categoryList = filter.get(constraint);
                ArrayList<String> filterList = new ArrayList<String>();
                ArrayList<String> filterItem = new ArrayList<String>();
                synchronized (this) {
                    filterItem.addAll(categoryList);
                }
                for(int i=0; i<filter.size(); i++){
                    filter=(HashMap<String, List<String>>) categories.get(i);
                    if((filter.get(i).toString().toLowerCase()).contains(constraint.toString().toLowerCase())){
                        filterList.addAll(categoryList);
                    }
                }
                results.count = filterList.size();
                results.values = filterList;
            }else{
                results.count = filter.size();
                results.values = filter;
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            //categories.clear();
            categoryList.clear();
            categories = (HashMap<String, List<String>>) results.values;
            //categories.addAll((Collection<? extends HashMap<String, List<String>>>) results.values); //null ?
            if(results.count > 0){

                notifyDataSetChanged();
            }else
                notifyDataSetInvalidated();
        }
    }
} //myExpandableListView
  

MainActivity.java

public class MainActivity extends ActionBarActivity implements OnQueryTextListener, OnCloseListener {

 public static HashMap<String, List<String>> categoryName;
 public static List<String> categoryList;
 ExpandableListView listexp;
 myExpandableListAdapter adapter;
 SearchView searchView;

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

    searchView=(android.widget.SearchView)findViewById(R.id.search);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setOnCloseListener(this);
    searchView.setIconifiedByDefault(false);
    searchView.setOnQueryTextListener(this);
    searchView.setQueryHint("search view");
    searchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            Toast.makeText(getBaseContext(), String.valueOf(hasFocus),
                    Toast.LENGTH_SHORT).show();
        }
    });

} // onCreate()

@Override
public boolean onQueryTextChange(String newText) {
    if(TextUtils.isEmpty(newText))
        exlv.clearTextFilter();
    else
        exlv.setFilterText(newText.toString());
    adapter.getFilter().filter(newText);
    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
   // TODO Auto-generated method stub
   return false;
}

@Override
public boolean onClose() {
   adapter.getFilter().filter("");
   return false;
}

} // MainActivity

有人可以帮我解决这个问题吗?

非常感谢你花时间解决这个问题

0 个答案:

没有答案