刷新listView项目android

时间:2017-09-13 07:59:50

标签: java android

我的问题是我在列表视图项上实现了可过滤的搜索视图。它运行良好但在执行搜索并且searchView为空之后,我无法获得之前的项目。我试过notifyDataSetChanged,但它对我不起作用。我该怎么办?

....
 private ListView mListNotes;
 NoteListAdapter na;

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListNotes = (ListView) findViewById(R.id.main_listview);
        mListNotes.setTextFilterEnabled(true);

    }
    @Override
    protected void onResume() {
        super.onResume();
        //load data into the listview and display
        mListNotes.setAdapter(na); 
   }

    @Override
    public boolean onQueryTextChange(String query) {
        if (!query.equals("")) {
            na.getFilter().filter(query);  // filter listView items when text is inputted.

        } else {
         na.notifyDataSetChanged();         // what do i put here to refresh my list view after search?

        }
        return false;
    }
}

// ListView Adapter


class NoteListAdapter extends ArrayAdapter<Note> implements Filterable{
    List<Note> objects;
    private List<Note> mStringFilterList;
     Filter filter;
    private static final int WRAP_CONTENT_LENGTH = 5;
    public NoteListAdapter(Context context, int resource, List<Note> objects) {
        super(context, resource, objects);
        this.objects = objects;
      this.mStringFilterList = objects;

    }

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

    @Nullable
   @Override
    public Note getItem(int position) {
       return objects.get(position);
    }

    @Override
   public long getItemId(int position) {
       return position;
   }


    @Override
    public Filter getFilter() {
        filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                ArrayList<Note> tempList= new ArrayList<>();
                FilterResults results = new FilterResults();
                if (constraint != null && objects != null) {
                    for(Note singleNote : objects) {
                        if( singleNote.getTitle().contains(constraint))
                            tempList.add(singleNote);
                    }
                    results.values = tempList;
                    results.count = tempList.size();
                }

                return results;
            }
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                objects = (ArrayList<Note>) results.values;
                notifyDataSetChanged();

            }
        };
        return filter;

        }


    }


}

2 个答案:

答案 0 :(得分:0)

您正在使用过滤后的数据替换原始数据。使用以下代码:

class NoteListAdapter extends ArrayAdapter<Note> implements Filterable {
        List<Note> objects;
        private List<Note> originalList = new ArrayList<>();
        Filter filter;
        private static final int WRAP_CONTENT_LENGTH = 5;

        public NoteListAdapter(Context context, int resource, List<Note> objects) {
            super(context, resource, objects);
            this.objects = objects;
            this.originalList.addAll(objects);
        }

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

        @Nullable
        @Override
        public Note getItem(int position) {
            return objects.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }


        @Override
        public Filter getFilter() {
            filter = new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    ArrayList<Note> tempList = new ArrayList<>();
                    FilterResults results = new FilterResults();
                    if (objects != null) {
                        if(constraint != null && !constraint.equals("")) {
                            for (Note singleNote : objects) {
                                if (singleNote.getTitle().contains(constraint))
                                    tempList.add(singleNote);
                            }
                            results.values = tempList;
                            results.count = tempList.size();
                        }else {
                            tempList.clear();
                            tempList.addAll(originalList);
                            results.values = tempList;
                            results.count = tempList.size();
                        }
                    }

                    return results;
                }

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    objects = (ArrayList<Note>) results.values;
                    notifyDataSetChanged();
                }
            };
            return filter;
        }
    }

希望有所帮助:)

答案 1 :(得分:0)

阅读代码需要更改或替换整个适配器类的注释  这个班级

class NoteListAdapter extends ArrayAdapter<Note> implements Filterable
 {
List<Note> objects;
private List<Note> mStringFilterList;
 Filter filter;
private static final int WRAP_CONTENT_LENGTH = 5;
public NoteListAdapter(Context context, int resource, List<Note> objects) {
    super(context, resource, objects);
    this.objects = objects;
  this.mStringFilterList = objects;

}

 @Override
  public int getCount() {
  return mStringFilterList.size();  // change this line
  }

 @Nullable
 @Override
 public Note getItem(int position) {
   return mStringFilterList.get(position);   // change this line
 }

@Override
public long getItemId(int position) {
   return position;
 }


@Override
public Filter getFilter() {
    filter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            ArrayList<Note> tempList= new ArrayList<>();
            FilterResults results = new FilterResults();
            if (constraint != null && constraint.length() > 0 && objects != null) {
                for(Note singleNote : objects) {
                    if( singleNote.getTitle().contains(constraint))
                        tempList.add(singleNote);
                }
                results.values = tempList;
                results.count = tempList.size();
            }

             // add else block. 
            else
            {
             results.values = objects;
                results.count = objects.size(); 
            }

            return results;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mStringFilterList = (ArrayList<Note>) results.values;  // change this line
            notifyDataSetChanged();

        }
    };
    return filter;

    }


}