在ListAdapter中实现筛选/搜索功能会返回UnsupportedOperationException。

huangapple go评论109阅读模式
英文:

Implementing Filter/Search functionality in ListAdapter returns UnsupportedOperationException

问题

我正在尝试在扩展ListAdapter的适配器类中实现搜索功能。我没有使用RecyclerAdapter,而是使用了ListAdapter。在主活动中,我使用EditTextaddTextChangedListener来实现搜索字段。我遇到了这个异常:

在ListAdapter中实现筛选/搜索功能会返回UnsupportedOperationException。

以下是我的代码:

  1. public class NoteAdapter extends ListAdapter<Note, NoteAdapter.NoteHolder> implements Filterable {
  2. private List<Note> mNoteListInit;
  3. private OnItemClickListener mListener;
  4. public NoteAdapter() {
  5. super(DIFF_CALLBACK);
  6. mNoteListInit = new ArrayList<>(getCurrentList());
  7. }
  8. @Override
  9. public Filter getFilter() {
  10. return exampleFilter;
  11. }
  12. public void setInitList(List<Note> noteList) {
  13. mNoteListInit = new ArrayList<>(noteList);
  14. }
  15. private Filter exampleFilter = new Filter() {
  16. @Override
  17. protected FilterResults performFiltering(CharSequence constraint) {
  18. List<Note> filteredList = new ArrayList<>();
  19. if (constraint == null || constraint.length() == 0) {
  20. filteredList.addAll(mNoteListInit);
  21. } else {
  22. String filterPattern = constraint.toString().toLowerCase().trim();
  23. for (Note note : mNoteListInit) {
  24. if (note.getTitle().toLowerCase().contains(filterPattern)) {
  25. filteredList.add(note);
  26. }
  27. }
  28. }
  29. FilterResults filterResults = new FilterResults();
  30. filterResults.values = filteredList;
  31. return filterResults;
  32. }
  33. @Override
  34. protected void publishResults(CharSequence constraint, FilterResults results) {
  35. submitList((List) results.values);
  36. }
  37. };
  38. }

在MainActivity中:

  1. mEditTextToolbarSearch.addTextChangedListener(new TextWatcher() {
  2. @Override
  3. public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
  4. @Override
  5. public void onTextChanged(CharSequence s, int start, int before, int count) { }
  6. @Override
  7. public void afterTextChanged(Editable s) {
  8. noteAdapter.getFilter().filter(s.toString());
  9. }
  10. });
  11. mNoteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() {
  12. @Override
  13. public void onChanged(List<Note> notes) {
  14. noteAdapter.submitList(notes);
  15. noteAdapter.setInitList(noteAdapter.getCurrentList());
  16. }
  17. });
英文:

I am trying to implement search functionality in my Adapter class that extends ListAdapter. I am not using RecyclerAdapter but instead I am using ListAdapter. In main activity to for searching field I am using EditText and addTextChangedListener. I am having this exception:

在ListAdapter中实现筛选/搜索功能会返回UnsupportedOperationException。

This is my code:

  1. public class NoteAdapter extends ListAdapter&lt;Note, NoteAdapter.NoteHolder&gt; implements Filterable {
  2. private List&lt;Note&gt; mNoteListInit;
  3. private OnItemClickListener mListener;
  4. public NoteAdapter() {
  5. super(DIFF_CALLBACK);
  6. exampleListFull = new ArrayList&lt;&gt;(getCurrentList());
  7. }
  8. @Override
  9. public Filter getFilter() {
  10. return exampleFilter;
  11. }
  12. public void setInitList(List&lt;Note&gt; noteList) {
  13. mNoteListInit = new ArrayList&lt;&gt;(noteList);
  14. }
  15. private Filter exampleFilter = new Filter() {
  16. @Override
  17. protected FilterResults performFiltering(CharSequence constraint) {
  18. List&lt;Note&gt; filteredList = new ArrayList&lt;&gt;();
  19. if (constraint == null || constraint.length() == 0) {
  20. filteredList.addAll(mNoteListInit);
  21. } else {
  22. String filterPattern = constraint.toString().toLowerCase().trim();
  23. for (Note note : mNoteListInit) {
  24. if (note.getTitle().toLowerCase().contains(filterPattern)) {
  25. filteredList.add(note);
  26. }
  27. }
  28. }
  29. FilterResults filterResults = new FilterResults();
  30. filterResults.values = filteredList;
  31. return filterResults;
  32. }
  33. @Override
  34. protected void publishResults(CharSequence constraint, FilterResults results) {
  35. submitList((List) results.values);
  36. }
  37. };

In MainActivity:

  1. mEditTextToolbarSearch.addTextChangedListener(new TextWatcher() {
  2. @Override
  3. public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
  4. @Override
  5. public void onTextChanged(CharSequence s, int start, int before, int count) { }
  6. @Override
  7. public void afterTextChanged(Editable s) {
  8. noteAdapter.getFilter().filter(s.toString());
  9. }
  10. });
  11. mNoteViewModel.getAllNotes().observe(this, new Observer&lt;List&lt;Note&gt;&gt;() {
  12. @Override
  13. public void onChanged(List&lt;Note&gt; notes) {
  14. noteAdapter.submitList(notes);
  15. noteAdapter.setInitList(noteAdapter.getCurrentList());
  16. }
  17. });

答案1

得分: 1

You are getting UnsupportedOperationException Due to using .clear() on UnmodifiableCollection

Looking into your code to see where you use clear():

  1. getCurrentList().clear();

So, definitly getCurrentList() is UnmodifiableCollection, so you can't modify it like normal Lists.

The documentation of getCurrentList() says:

The returned list may not be mutated - mutations to content must be done through submitList(List).

So, you can mutate the ListAdapter getCurrentList() through submitList

So, try to replace getCurrentList().clear() with submitList(new ArrayList())

英文:

You are getting UnsupportedOperationException Due to using .clear() on UnmodifiableCollection

Looking into your code to see where you use clear():

  1. getCurrentList().clear();

So, definitly getCurrentList() is UnmodifiableCollection, so you can't modify it like normal Lists.

The documentation of getCurrentList() says:
> The returned list may not be mutated - mutations to content must be done through submitList(List).

So, you can mutate the ListAdapter getCurrentList() through submitList

So, try to replace getCurrentList().clear() with submitList(new ArrayList())

huangapple
  • 本文由 发表于 2020年8月2日 07:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63211078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定