使用带有搜索视图的 LiveData 项目过滤 RecyclerView 的列表 [英] Filter RecyclerView's list with LiveData items with searchview
问题描述
我决定优化我的代码,因此切换到 liveData.我遵循了 youtube 上的教程(youtube 链接)但我不太明白如何在用户输入单词时过滤我的 recyclerView,因为我没有在我的适配器中存储任何列表.我在 MainActivity 上使用了一个简单的搜索视图过滤系统.
I decided to optimize my code and therefore switch to liveData. I followed a tutorial on youtube (youtube link) but I do not quite understand how I can filter my recyclerView when the user enters a word since I do not store any list in my Adapter. I use a simple searchview filter system on my MainActivity.
此外,我使用 DiffUtil 更新我的 recyclerView 并更新我的适配器,感谢:
Moreover, I use DiffUtil to update my recyclerView and I update my Adapter thanks to:
noteViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(NoteViewModel.class);
noteViewModel.getAllNotes().observe(this, adapter::submitList);
我的代码与视频几乎相同,但这是其中的一部分:
My code is almost identical to the video but here is a part of it:
视图模型:
public class NoteViewModel extends AndroidViewModel {
private NoteRepository repository;
private LiveData<List<Note>> allNotes;
public NoteViewModel(@NonNull Application application) {
super(application);
repository = new NoteRepository(application);
allNotes = repository.getAllNotes();
}
public void insert(Note note) {
repository.insert(note);
}
public void update(Note note) {
repository.update(note);
}
public void delete(List<Note> notes) {
repository.delete(notes);
}
public LiveData<List<Note>> getAllNotes() {
return allNotes;
}
}
我的仓库:
public class NoteRepository {
private NotesDAO notesDAO;
private LiveData<List<Note>> allNotes;
public NoteRepository(Application application) {
NotesDB database = NotesDB.getInstance(application);
notesDAO = database.notesDAO();
allNotes = notesDAO.getAllNotes();
}
public void insert(Note note) {
new InsertNoteAsyncTask(notesDAO).execute(note);
}
public void update(Note note) {
new UpdateNoteAsyncTask(notesDAO).execute(note);
}
public void delete(List<Note> note) {
new DeleteNoteAsyncTask(notesDAO).execute(note);
}
public LiveData<List<Note>> getAllNotes() {
return allNotes;
}
private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Void> { // SOME STUFF }
private static class UpdateNoteAsyncTask extends AsyncTask<Note, Void, Void> { // SOME STUFF }
private static class DeleteNoteAsyncTask extends AsyncTask<List<Note>, Void, Void> { // SOME STUFF }
}
推荐答案
最后,感谢@EpicPandaForce,我做到了:
Finally, thanks to @EpicPandaForce, I did that:
我的视图模型:
public class NoteViewModel extends AndroidViewModel {
private NoteRepository repository;
private final LiveData<List<Note>> allNotes;
private MutableLiveData<String> filterText = new MutableLiveData<>();
public NoteViewModel(@NonNull Application application) {
super(application);
repository = new NoteRepository(application);
allNotes = Transformations.switchMap(filterText, (input) ->
{
if(input == null || input.equals(""))
return repository.getAllNotes();
else
return repository.filter(input);
});
}
public void setFilter(String query) {
filterText.setValue(query);
}
public LiveData<List<Note>> getAllNotes() {
return allNotes;
}
}
在我的存储库中:
public LiveData<List<Note>> filter(String input) {
try {
return new FilterNoteAsyncTask(notesDAO).execute(input).get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
return null;
}
private static class FilterNoteAsyncTask extends AsyncTask<String, Void, LiveData<List<Note>>> {
private NotesDAO notesDAO;
private FilterNoteAsyncTask(NotesDAO notesDAO) {
this.notesDAO = notesDAO;
}
@Override
protected LiveData<List<Note>> doInBackground(String... strings) {
return notesDAO.filter(strings[0]);
}
}
我在数据库中执行请求感谢:
And I perform the request in the database thanks to:
@Query("SELECT * FROM note_table WHERE LOWER(title) LIKE '%' || :search || '%'")
LiveData<List<Note>> filter(String search);
这篇关于使用带有搜索视图的 LiveData 项目过滤 RecyclerView 的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!