android searchview无法用大写字母过滤

时间:2015-07-09 09:03:01

标签: android filter adapter searchview

我有searchview,当我输入英文字母小字母时,它工作正常。但是,当我使用大写单词过滤列表时,它不起作用,当我键入时,请帮助我解决我的问题。谢谢我提前

这是我的searchView代码

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    // Inflate menu to add items to action bar if it is present.
    inflater.inflate(R.menu.menu_main, menu);
    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView =
            (android.support.v7.widget.SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

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

        @Override
        public boolean onQueryTextChange(String newText) {
            Log.i("search", "search is working  "+newText);
            listDataAdapter.getFilter().filter(newText);
            return false;
        }
    });

    return true;
}

这是适配器代码

 public abstract class ListDataAdapter extends ArrayAdapter implements Filterable {

    List list = new ArrayList();
    List filterlist = new ArrayList();
    boolean index[];

    public ListDataAdapter(Context context, int resource) {

        super(context, resource);
        index = new boolean[list.size()];


    }

    static class LayoutHandler {
        TextView name, quantity, calorie, fat, protein, sugar, carbohydrates;
        CheckBox names;

    }

    @Override
    public void add(Object object) {

        super.add(object);
        list.add(object);
        filterlist.add(object);
        index = new boolean[list.size()];
    }

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

    @Override
    public Object getItem(int position) {
        // return list.get(position);
        return filterlist.get(position);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        LayoutHandler layoutHandler;
        //if (row == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.row_layout, parent, false);
        layoutHandler = new LayoutHandler();
        layoutHandler.name = (TextView) row
                .findViewById(R.id.text_dish_name);
        layoutHandler.quantity = (TextView) row
                .findViewById(R.id.text_dish_quantity);
        layoutHandler.calorie = (TextView) row
                .findViewById(R.id.text_dish_calorie);
        layoutHandler.fat = (TextView) row.findViewById(R.id.text_dish_fat);
        layoutHandler.protein = (TextView) row
                .findViewById(R.id.text_dish_protein);
        layoutHandler.sugar = (TextView) row
                .findViewById(R.id.text_dish_sugar);
        layoutHandler.carbohydrates = (TextView) row
                .findViewById(R.id.text_dish_carbohydrates);
        layoutHandler.names = (CheckBox) row.findViewById(R.id.checkBox1);

        row.setTag(layoutHandler);

        //} else {
        layoutHandler = (LayoutHandler) row.getTag();

        //  }
        final DataProvider dataProvider = (DataProvider) this.getItem(position);
        layoutHandler.name.setText(dataProvider.getName());
        layoutHandler.quantity.setText(dataProvider.getQuantity());
        layoutHandler.calorie
                .setText(String.valueOf(dataProvider.getCalorie()));
        layoutHandler.fat.setText(dataProvider.getFat());
        layoutHandler.protein.setText(dataProvider.getProtein());
        layoutHandler.sugar.setText(dataProvider.getSugar());
        layoutHandler.carbohydrates.setText(dataProvider.getCarbohydrates());

        layoutHandler.names.setChecked(dataProvider.isSelected());
        layoutHandler.names.setTag(position);
        //layoutHandler.names.setChecked(index[position]);
        layoutHandler.names
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        // TODO Auto-generated method stub
                        int pos = (Integer) buttonView.getTag();
                        index[pos] = isChecked;
                        showCheckedButton(position, isChecked);
                        //dataProvider.setSelected(isChecked);
                        filterlist.set(position, dataProvider);
                        //ListDataAdapter.this.notifyDataSetChanged();
                        //list.set(position,dataProvider);

                    }
                });

        return row;
    }

    public ArrayList<DataProvider> getSelectedIndexes() {
        int size = list.size();

        ArrayList<DataProvider> selectedItems = new ArrayList<DataProvider>();
        for (int i = 0; i < size; i++) {
            DataProvider cItem = (DataProvider) list.get(i);
            if (cItem.isSelected()) {
                selectedItems.add(cItem);
            }
        }

        return selectedItems;
    }
    @Override
    public Filter getFilter() {
        return new Filter() {
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                //  Log.d(Constants.TAG, "**** PUBLISHING RESULTS for: " + constraint);
                filterlist = (List<DataProvider>) results.values;
                ListDataAdapter.this.notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                //  Log.d(Constants.TAG, "**** PERFORM FILTERING for: " + constraint);
                List<DataProvider> filteredResults = getFilteredResults(constraint);
                FilterResults results = new FilterResults();
                results.values = filteredResults;
                return results;
            }
        };
    }

    private List<DataProvider> getFilteredResults(CharSequence constraint) {
        String filterableString;
        List<DataProvider> filteredList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String temp = ((DataProvider) list.get(i)).getName();
            if (temp.toString().toLowerCase().contains(constraint)) {
                filteredList.add((DataProvider) list.get(i));
            }
        }
        return filteredList;
    }

    protected abstract void showCheckedButton(int position, boolean value);
}

2 个答案:

答案 0 :(得分:6)

尝试替换适配器中的以下代码行

if (temp.toString().toLowerCase().contains(constraint)) {
                filteredList.add((DataProvider) list.get(i));
 }

if (temp.toString().toLowerCase().contains(constraint.toString().toLowerCase())) {
                filteredList.add((DataProvider) list.get(i));
  }

您尚未将约束转换为小写。

答案 1 :(得分:1)

你所经历的是正确的。您正在将temp.toLowerCase()与以大写字母开头的constraint进行比较。条件总是错误的。根据您的要求,constraint必须转换 toLowerCase()