android searchview 不能用大写字母过滤
android searchview can not filter with capital letter
我有搜索视图,当我输入英文单词的小写字母时它工作正常。但是当我使用大写单词来过滤列表时它不起作用当我键入时没有任何反应请帮助我解决我的问题。谢谢我提前
这是我的 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);
}
您所遇到的是正确的。您正在将 temp.toLowerCase()
与以大写字母开头的 constraint
进行比较。条件总是假的。根据您的要求,constraint
也必须 转换 toLowerCase()
尝试替换适配器中的以下代码行
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));
}
您还没有将约束转换为小写。
我有搜索视图,当我输入英文单词的小写字母时它工作正常。但是当我使用大写单词来过滤列表时它不起作用当我键入时没有任何反应请帮助我解决我的问题。谢谢我提前
这是我的 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);
}
您所遇到的是正确的。您正在将 temp.toLowerCase()
与以大写字母开头的 constraint
进行比较。条件总是假的。根据您的要求,constraint
也必须 转换 toLowerCase()
尝试替换适配器中的以下代码行
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));
}
您还没有将约束转换为小写。