-1

I want to refresh my adapter in two situation but nothing happend. First I tried called

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

The second I tried refresh it in my filter by notifyDataSetInvalidated();

My Adapter Class:

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

UPDATE: I solved my problem by adding two functions:

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

One is refreshing all items the second one adding to the list. I supose this is not very efficient solution. I will be grateful if someone give me a hint how improve that.

Expiredmind
  • 788
  • 1
  • 8
  • 29
  • what is a reasons of using ArrayAdapter as base class? `ArrayAdapter.clear()` is invalidating dataset, what is the point of `notifyDataSetChanged` before? ... same story with `ArrayAdapter.add()` – Selvin Sep 22 '16 at 09:32
  • i thought I should erase all items in adapter by adapter.clear() than add new one and notify changes. – Expiredmind Sep 22 '16 at 09:37
  • `@Override public int getCount() { return records.size(); } ... public View getView(int position, View view, ViewGroup parent) { final Exhibition exhibition= records.get(position);` you are not using default implementation of `AA.getCount` ... and you are not using `AA.getItem()` inside your `AA.getView()` implementation ... so using `AA.clear` and `AA.add` doesn't make sens – Selvin Sep 22 '16 at 09:40
  • After removing getCount() the AA.clear and AA.add works and list is refreshing but it crash in filter using by throwing IndexOutOfBoundsException:| – Expiredmind Sep 22 '16 at 10:02
  • So do you have some ideas how to refresh the list and leave the getCount? – Expiredmind Sep 22 '16 at 11:34

1 Answers1

-1

try this:

    filteredList.clear();

    adapter.notifyDataSetChanged();
Damini Mehra
  • 3,257
  • 3
  • 13
  • 24
  • 1
    He could also try to stand on his head ... the point is: what for? ... getCount and getView is using `records` which is not connected with `filteredList` ... clearing `filteredList` and then calling `notifyDataSetChanged` doesn't make sens at all... – Selvin Sep 22 '16 at 09:33