NotifyDataSetChanged() 未显示更新列表
NotifyDataSetChanged() isn't showing the updated list
我在 publishResults() 函数的过滤列表中得到了正确的对象,但是过滤后的列表没有显示在屏幕上。
以下是继承自 ArrayAdapter class 的 CityAdapter class 的代码。
好像 notifyDataSetChanged() 不工作?
请告知为什么列表在代码中不是displaying.What的错误。
import...
public class CityListAdapter extends ArrayAdapter<City> implements Filterable {
private ArrayList<City> cities;
private ArrayList<City> filtered_list;
public CityListAdapter(Context context, int resource, ArrayList<City>cities) {
super(context, resource, cities);
this.cities = cities;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, ViewGroup parent) {
Holder holder = new Holder();
if(convertView == null) {
LayoutInflater inflator = LayoutInflater.from(getContext());
convertView = inflator.inflate(R.layout.adapter_view,parent,false);
holder.box = (CheckBox) convertView.findViewById(R.id.checkBox);
holder.name = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(holder);
}
else {
holder = (Holder) convertView.getTag();
}
City city = getItem(position) ;
holder.name.setText(city.getName());
return convertView;
}
private class Holder {
CheckBox box;
TextView name;
}
@NonNull
@Override
public Filter getFilter() {
return new CityFilter();
}
private class CityFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
filtered_list = new ArrayList<>();
if (constraint != null && constraint.length()!= 0) {
for (int i = 0; i < cities.size(); i++) {
if (cities.get(i).getName().contains(constraint)) {
filtered_list.add(cities.get(i));
showMessage(cities.get(i).getName() + "added");
}
}
}
results.values = filtered_list;
results.count = filtered_list.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filtered_list = (ArrayList<City>) results.values;
int size =filtered_list.size();
showMessage("Data set changed"+String.format("%d", size));
notifyDataSetChanged();
}
}
public void showMessage(String message) {
Toast toast = Toast.makeText(getContext(), message, Toast.LENGTH_LONG);
toast.show();
}
}
您需要在最开始(即在构造函数中)将完整的项目列表分配给过滤列表;这样他们就可以在没有过滤结果(空搜索)时拥有整个列表,并且只要您实例化适配器就会发生这种情况。
public CityListAdapter(Context context, int resource, ArrayList<City>cities) {
super(context, resource, cities);
this.cities = cities;
this.filtered_list = cities; // <<<<< Change here
}
旁注
同时覆盖 getCount()
以避免 IndexOutOfBoundsException
当原始列表大小不等于过滤后的列表大小时滚动到列表末尾。
由于原列表为cities
,过滤时,其大小会缩小,所以只需要获取过滤后的列表大小,而不是原始列表大小即可避免IndexOutOfBoundsException
。
因此将其添加到适配器中:
@Override
public int getCount() {
return filtered_list.size();
}
我在 publishResults() 函数的过滤列表中得到了正确的对象,但是过滤后的列表没有显示在屏幕上。 以下是继承自 ArrayAdapter class 的 CityAdapter class 的代码。 好像 notifyDataSetChanged() 不工作? 请告知为什么列表在代码中不是displaying.What的错误。
import...
public class CityListAdapter extends ArrayAdapter<City> implements Filterable {
private ArrayList<City> cities;
private ArrayList<City> filtered_list;
public CityListAdapter(Context context, int resource, ArrayList<City>cities) {
super(context, resource, cities);
this.cities = cities;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, ViewGroup parent) {
Holder holder = new Holder();
if(convertView == null) {
LayoutInflater inflator = LayoutInflater.from(getContext());
convertView = inflator.inflate(R.layout.adapter_view,parent,false);
holder.box = (CheckBox) convertView.findViewById(R.id.checkBox);
holder.name = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(holder);
}
else {
holder = (Holder) convertView.getTag();
}
City city = getItem(position) ;
holder.name.setText(city.getName());
return convertView;
}
private class Holder {
CheckBox box;
TextView name;
}
@NonNull
@Override
public Filter getFilter() {
return new CityFilter();
}
private class CityFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
filtered_list = new ArrayList<>();
if (constraint != null && constraint.length()!= 0) {
for (int i = 0; i < cities.size(); i++) {
if (cities.get(i).getName().contains(constraint)) {
filtered_list.add(cities.get(i));
showMessage(cities.get(i).getName() + "added");
}
}
}
results.values = filtered_list;
results.count = filtered_list.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filtered_list = (ArrayList<City>) results.values;
int size =filtered_list.size();
showMessage("Data set changed"+String.format("%d", size));
notifyDataSetChanged();
}
}
public void showMessage(String message) {
Toast toast = Toast.makeText(getContext(), message, Toast.LENGTH_LONG);
toast.show();
}
}
您需要在最开始(即在构造函数中)将完整的项目列表分配给过滤列表;这样他们就可以在没有过滤结果(空搜索)时拥有整个列表,并且只要您实例化适配器就会发生这种情况。
public CityListAdapter(Context context, int resource, ArrayList<City>cities) {
super(context, resource, cities);
this.cities = cities;
this.filtered_list = cities; // <<<<< Change here
}
旁注
同时覆盖 getCount()
以避免 IndexOutOfBoundsException
当原始列表大小不等于过滤后的列表大小时滚动到列表末尾。
由于原列表为cities
,过滤时,其大小会缩小,所以只需要获取过滤后的列表大小,而不是原始列表大小即可避免IndexOutOfBoundsException
。
因此将其添加到适配器中:
@Override
public int getCount() {
return filtered_list.size();
}