简体   繁体   English

NotifyDataSetChanged() 未显示更新的列表

[英]NotifyDataSetChanged() isn't showing the updated list

I get correct objects in filtered list in publishResults() function but the filtered list is not displayed on the screen after filtering.我在 publishResults() function 的过滤列表中得到正确的对象,但过滤后的过滤列表未显示在屏幕上。 Following is the code for CityAdapter class inhereted from ArrayAdapter class.以下是从 ArrayAdapter class 继承的 CityAdapter class 的代码。 IT seems like notifyDataSetChanged() isn't working?它似乎 notifyDataSetChanged() 不起作用? Please inform why list is not displaying.What's wrong in the code.请告知为什么列表没有显示。代码有什么问题。


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();
    }

}

You need to have a full list of items in the very beginning (ie in the constructor) assigned to the filtered list;您需要在最开始(即在构造函数中)分配给过滤列表的完整项目列表; so that they can have the entire list when there is no filtering of results (empty search) and that occurs whenever you instantiate the adapter.这样他们就可以在没有过滤结果(空搜索)时拥有整个列表,并且每当您实例化适配器时都会发生这种情况。

public CityListAdapter(Context context, int resource, ArrayList<City>cities) {
    super(context, resource, cities);
    this.cities = cities;
    this.filtered_list = cities; // <<<<< Change here
}

Side Note边注

Also override getCount() to avoid IndexOutOfBoundsException when reaching the end of the list on scroll whenever the original list size not equal to the filtered list size.当原始列表大小不等于过滤后的列表大小时,还覆盖getCount()以避免在滚动时到达列表末尾时出现IndexOutOfBoundsException

As the original list is cities , when you make filtering, its size will shrink down, so you need only to get the filtered list size not the original list size to avoid IndexOutOfBoundsException .由于原始列表是cities ,当您进行过滤时,它的大小会缩小,因此您只需要获取过滤后的列表大小而不是原始列表大小,以避免出现IndexOutOfBoundsException

So add this to the adapter:因此,将其添加到适配器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM