适配器不刷新

时间:2016-09-22 09:24:09

标签: android refresh adapter notify notifydatasetchanged

我想在两种情况下刷新我的适配器,但没有任何事情发生。首先我试着叫

 adapter.clear();
 adapter.addAll(filteredList); //filtered list is a new list to show on adapter 
 adapter.notifyDataSetChanged();

我尝试过的第二个按notifyDataSetInvalidated();

在我的过滤器中刷新它

我的适配器类:

public class DatabaseAdapter extends ArrayAdapter<Exhibition>  {

    private Context context;
    private List<Exhibition> records;
    private List<Exhibition>filterRecords;
    private Dao<Exhibition, Integer> exhibitionDAO;
    private ViewHolder viewHolder;
    private SearchFilter searchFilter;

    public DatabaseAdapter(Context context,
                           int itemView,
                           List objects,
                           Dao<Exhibition, Integer> exhibitionDAO) {
        super(context, itemView, objects);
        this.context=context;
        this.records = new ArrayList<>(objects);
        this.filterRecords=new ArrayList<>(objects);
        this.searchFilter=new SearchFilter();
        this.exhibitionDAO = exhibitionDAO;

    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        final Exhibition exhibition= records.get(position);

        if(view==null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view= inflater.inflate(R.layout.museum_list_item_layout,parent,false);
            viewHolder = new ViewHolder();
            view.setTag(viewHolder);
        }else
            viewHolder = (ViewHolder) view.getTag();
        viewHolder.eventName=(TextView)view.findViewById(R.id.list_item);
            viewHolder.eventName.setText(exhibition.getName());

        return view;
    }

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

    @Override
    public Filter getFilter() {
        if(searchFilter==null)
            searchFilter=new SearchFilter();
        return searchFilter;
    }

    public class ViewHolder{
        TextView eventName;
    }

    private class SearchFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            List<Exhibition> list;
            if (constraint == null || constraint.length() == 0){
                list = new ArrayList<Exhibition>(records);
                results.values = list;
                results.count = list.size();}
                else{
         try{

             list  = exhibitionDAO.queryBuilder().where().like("name","%"+constraint+"%").query();
             synchronized (list){
             results.values=list;
             results.count=list.size();}
         }
         catch (SQLException e){e.printStackTrace();}}
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filterRecords = (ArrayList<Exhibition>)results.values;
            notifyDataSetChanged();
            clear();
            int count = filterRecords.size();

            for(int i = 0; i<count; i++){
                add(filterRecords.get(i));
                notifyDataSetInvalidated();
            }
        }
    }
    }

更新:我通过添加两个函数解决了我的问题:

public void addAllItems(List< Exhibition> exhibitions){
        records.clear();
        records.addAll(exhibitions);
    }
    public void addItems(List<Exhibition> exhibitions){
        records.addAll(exhibitions);
    }

一个是刷新添加到列表中的第二个项目。我认为这不是一个非常有效的解决方案。如果有人给我提示如何改进,我将不胜感激。

1 个答案:

答案 0 :(得分:-1)

试试这个:

    filteredList.clear();

    adapter.notifyDataSetChanged();