Ariana
Ariana

Reputation: 41

Search Filter in RecyclerView not showing anything

My app consists in letting you add lists in which you can keep your notes. Therefore, I have this NotesListActivity where I can add and keep my Lists. I wanted to filter this lists following the https://www.youtube.com/watch?v=CTvzoVtKoJ8 tutorial and then I tried to adapt it to my code like below. Could you please tell me what is the problem here, cause I don't even get an error, I just not get any title of list as result. So, this is what I have in my RecyclerAdapter:

public class NotesRecyclerAdapter extends RecyclerView.Adapter<NotesRecyclerAdapter.ViewHolder> implements Filterable {

    private static final String TAG = "NotesRecyclerAdapter";
    ArrayList<Note> notesListAll;

    private ArrayList<Note> mNotes;
    private OnNoteListener mOnNoteListener;

    public NotesRecyclerAdapter(ArrayList<Note> mNotes, OnNoteListener onNoteListener) {
        this.mNotes = mNotes;
        this.mOnNoteListener = onNoteListener;
        this.notesListAll = new ArrayList<>();
        notesListAll.addAll(mNotes);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       ...
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
       ...
    }

    @Override
    public int getItemCount() {
        return mNotes.size(); 
    }

    @Override
    public Filter getFilter() { return myFilter; }

    Filter myFilter = new Filter() {

        //runs on background thread
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {

            List<Note> filteredList = new ArrayList<>();

            if (charSequence == null || charSequence.length() == 0) {

                filteredList.addAll(notesListAll);
            } else {

                for (Note note : notesListAll) {

                    if (note.getTitle().toLowerCase().contains(charSequence.toString().toLowerCase())) {
                        filteredList.add(note);

                    }
                }
            }

            FilterResults filterResults = new FilterResults();
            filterResults.values = filteredList;

            return filterResults;
        }

        //runs on a ui thread
        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {

            mNotes.clear();
            mNotes.addAll((Collection <? extends Note>) filterResults.values );

            notifyDataSetChanged();
        }
    };



    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

       ...
        }

        @Override
        public void onClick(View view) {
            ...
        }
    }

    public interface OnNoteListener{
        void onNoteClick(int position);
    }
}

And this in my Activity:

public class NotesListActivity extends AppCompatActivity implements
        NotesRecyclerAdapter.OnNoteListener,
        FloatingActionButton.OnClickListener
{

    private static final String TAG = "NotesListActivity";


    private RecyclerView mRecyclerView;


    private ArrayList<Note> mNotes = new ArrayList<>();
    private NotesRecyclerAdapter mNoteRecyclerAdapter;
    private NoteRepository mNoteRepository;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
       ...

        initRecyclerView();
        mNoteRepository = new NoteRepository(this);
        retrieveNotes();

        setSupportActionBar((Toolbar)findViewById(R.id.notes_toolbar));
        setTitle("Notes");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.search,menu);
        MenuItem item = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) item.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

             //   NotesRecyclerAdapter.getFilter().filter(newText);
                mNoteRecyclerAdapter.getFilter().filter(newText);
                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }

    private void retrieveNotes() {
        mNoteRepository.retrieveNotesTask().observe(this, new Observer<List<Note>>() {
            @Override
            public void onChanged(@Nullable List<Note> notes) {
               ...
            }
        });
    }


    private void initRecyclerView(){
      ...
    }


    @Override
    public void onNoteClick(int position) {
...
    }

    @Override
    public void onClick(View view) {
       ...
    }

    private void deleteNote(Note note) {
       ...
    }

    ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            deleteNote(mNotes.get(viewHolder.getAdapterPosition()));
        }
    };
}

Upvotes: 0

Views: 767

Answers (3)

imalixch
imalixch

Reputation: 53

I just solved it out this way

notesListAll.addAll(mNotes);

to

notesListAll = mNotes;

Upvotes: 0

Lida
Lida

Reputation: 26

You need to check if it's the first intance or not.

if (firstInstance) {notesList.clear();}
       notesList.addAll(notesListAll);

Upvotes: 1

Zain
Zain

Reputation: 40908

The problem is that you are using an empty notesListAll list for filtering results; you need to populate it with the list of notes in the constructor

public NotesRecyclerAdapter(ArrayList<Note> mNotes, OnNoteListener onNoteListener) {
    this.mNotes = mNotes;
    this.mOnNoteListener = onNoteListener;
    this.notesListAll = new ArrayList<>(); // <---- Empty list
    notesListAll.addAll(mNotes); // <--- Fix is here
}

Also, you're filtering based on getContent(), probably the searched text is not a part of that content, so you need to filter out some other results within the Note data class.


 if (note.getContent().toLowerCase().contains(charSequence.toString().toLowerCase())) {

Upvotes: 1

Related Questions