public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
FilteredData = ArrayData;
} else {
ArrayList<Data> filteredList = new ArrayList<>();
for (Data checkData: ArrayData) {
if (checkData.getNames().toLowerCase().contains(charString)) {
filteredList.add(checkData);
}
}
FilteredData = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = FilteredData;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
FilteredData = (ArrayList<Data>) filterResults.values;
notifyDataSetChanged();
}
};
}
这是我的数据对象:
public class Data {
public String names;
public String cPoints;
public Double latitude;
public Double longitude;
public String code;
public Float distance;
public Integer spots;
public int scale = 2;
public Data(String n, String cP, Double lat, Double lon, String c, Float dis, Integer sp ){
names = n;
cPoints = cP;
latitude = lat;
longitude = lon;
code = c;
distance = dis;
spots = sp;
}
public String getNames(){
return names;
}
public String getcPoints(){
return cPoints;
}
public Double getLatitude(){
return latitude;
}
public Double getLongitude(){
return longitude;
}
public String getCode(){
return code;
}
public Integer getSpots(){
return spots;
}
public Double getDistance(){
return Math.round(distance * Math.pow(10, scale)) / Math.pow(10, scale);
}
} 数据对象用于将单个停车场的项目彼此连接
我已经创建了这个过滤器,我这样称呼它:adapter.getFilter.filter(newText)
但不知怎的,我的适配器RecyclerView没有改变,有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
您需要在代码中进行一些修改。您将新过滤的数据插入到不同的arrylist中,因此在调用notifydatasetchanged()之后,它将不会过滤您的数据。
首先将旧列表数据声明为另一个列表变量,然后按以下方式进行更改
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
AllData = ArrayData;
if (charText.length() == 0) {
ArrayData = AllData;
} else {
ArrayData.clear();
for (String s : AllData) {
if (s.toLowerCase(Locale.getDefault()).contains(charText)) {
ArrayData.add(s);
}
}
}
notifyDataSetChanged();
}
然后在搜索中调用此过滤器方法。
希望这有用..
@ Rajesh