我有一个列表视图显示区域列表。此列表视图也包含部分。
我想了解如何隐藏搜索结果中的部分标签?或者如何禁用单击部分。
我想只搜索没有部分的搜索后列表中的项目。
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextChange(String newText) {
ArrayList<Item> tempList = new ArrayList<>();
for(Item temp : countryList){
if(temp.getTitle().toString().toLowerCase().contains(newText.toLowerCase())) {
tempList.add(temp);
//Toast.makeText(getApplicationContext(), "hani", Toast.LENGTH_LONG).show();
}
}
CountryAdapter adapter = new CountryAdapter(getApplicationContext(), tempList);
lvCountry.setAdapter(adapter);
//adapter.getFilter().filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
});
好吧,如果您使用搜索视图和ListView与您的项目,您可以始终使用SearchView上面的方法来过滤适配器
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
您需要在自定义适配器内部
public class SearchAddressAdapter extends BaseAdapter implements Filterable {
private List<AddressItemModel> items;
@Override
public Filter getFilter() {
if(valueFilter==null) {
valueFilter=new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
//Invoked in a worker thread to filter the data according to the constraint.
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results=new FilterResults();
if(constraint!=null && constraint.length()>0){
List<AddressItemModel> filterList=new ArrayList<AddressItemModel>();
for(int i=0;i<mStringFilterList.size();i++){
if((mStringFilterList.get(i).getTitle().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
AddressItemModel contacts = new AddressItemModel();
contacts.setTitle(mStringFilterList.get(i).getTitle());
filterList.add(contacts);
}
}
results.count=filterList.size();
results.values=filterList;
}else{
results.count=mStringFilterList.size();
results.values=mStringFilterList;
}
return results;
}
//Invoked in the UI thread to publish the filtering results in the user interface.
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
items = (ArrayList<AddressItemModel>) results.values;
notifyDataSetChanged();
}
}
这几乎就是:)