Android studio 过滤器回收视图';使用searchview创建包含LiveData项的列表

Android studio 过滤器回收视图';使用searchview创建包含LiveData项的列表,android-studio,filter,android-recyclerview,viewmodel,android-livedata,Android Studio,Filter,Android Recyclerview,Viewmodel,Android Livedata,我决定优化我的代码,因此切换到liveData。我在youtube()上学习了一个教程,但我不太明白当用户输入单词时如何过滤我的recyclerView,因为我没有在适配器中存储任何列表。我在MainActivity上使用了一个简单的searchview过滤系统 此外,我使用DiffUtil更新我的recyclerView,并更新我的适配器,这要感谢: noteViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplic

我决定优化我的代码,因此切换到liveData。我在youtube()上学习了一个教程,但我不太明白当用户输入单词时如何过滤我的recyclerView,因为我没有在适配器中存储任何列表。我在MainActivity上使用了一个简单的searchview过滤系统


此外,我使用DiffUtil更新我的recyclerView,并更新我的适配器,这要感谢:

noteViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(NoteViewModel.class);
noteViewModel.getAllNotes().observe(this, adapter::submitList);
@Query("SELECT * FROM note_table WHERE LOWER(title) LIKE '%' || :search || '%'")
LiveData<List<Note>> filter(String search);
我的代码与视频几乎相同,但以下是其中的一部分:

视图模型:

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 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;
        }
}
公共类NoteViewModel扩展了AndroidViewModel{
私人记事本储存库;
私有LiveData-allNotes;
public NoteViewModel(@NonNull应用程序){
超级(应用);
repository=新的NoteRepository(应用程序);
allNotes=repository.getAllNotes();
}
公共作废插入(注){
储存库。插入(注);
}
公共作废更新(注){
储存库。更新(注);
}
公共作废删除(列表注释){
删除(注释);
}
公共LiveData getAllNotes(){
归还所有票据;
}
}
我的存储库:

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 }

}
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]);
        }
    }
公共类记事本存储库{
私人债券;
私有LiveData-allNotes;
公共记事本存储库(应用程序){
NotesDB数据库=NotesDB.getInstance(应用程序);
notesDAO=database.notesDAO();
allNotes=notesDAO.getAllNotes();
}
公共作废插入(注){
新建InsertNoteAsyncTask(notesDAO)。执行(note);
}
公共作废更新(注){
新的UpdateNodeAsynctask(notesDAO).execute(note);
}
公共作废删除(列表注释){
新建DeleteNoteAsyncTask(notesDAO)。执行(note);
}
公共LiveData getAllNotes(){
归还所有票据;
}
私有静态类InsertNoteAsyncTask扩展了AsyncTask{//SOME STUFF}
私有静态类UpdateNotateSyncTask扩展了AsyncTask{//SOME STUFF}
私有静态类DeleteNoteAsyncTask扩展了AsyncTask{//SOME STUFF}
}

最后,感谢@EpicPandaForce,我做到了:

我的ViewModel:

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 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;
        }
}
公共类NoteViewModel扩展了AndroidViewModel{
私人记事本储存库;
私有最终LiveData allNotes;
私有MutableLiveData filterText=新的MutableLiveData();
public NoteViewModel(@NonNull应用程序){
超级(应用);
repository=新的NoteRepository(应用程序);
allNotes=Transformations.switchMap(filterText,(输入)->
{
if(input==null | | input.equals(“”)
返回repository.getAllNotes();
其他的
返回repository.filter(输入);
});
}
公共void setFilter(字符串查询){
filterText.setValue(查询);
}
公共LiveData getAllNotes(){
归还所有票据;
}
}
在我的存储库中:

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 }

}
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]);
        }
    }
public LiveData过滤器(字符串输入){
试一试{
返回新的FilterNoteAsyncTask(notesDAO).execute(input.get();
}捕获(ExecutionException | InterruptedException e){
e、 printStackTrace();
}
返回null;
}
私有静态类FilterNodeAsyncTask扩展了AsyncTask{
私人债券;
专用筛选器异步任务(NotesDAO NotesDAO){
this.notesDAO=notesDAO;
}
@凌驾
受保护的LiveData doInBackground(字符串…字符串){
返回notesDAO.filter(字符串[0]);
}
}
由于以下原因,我在数据库中执行请求:

noteViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(NoteViewModel.class);
noteViewModel.getAllNotes().observe(this, adapter::submitList);
@Query("SELECT * FROM note_table WHERE LOWER(title) LIKE '%' || :search || '%'")
LiveData<List<Note>> filter(String search);
@Query(“从note_表中选择*,其中较低的(标题)如“%”搜索“%”)
LiveData过滤器(字符串搜索);

注意:
新建ViewModelProvider.AndroidViewModelFactory(getApplication()).create(NoteViewModel.class)
。我尝试了此操作,但出现了一个错误:
java.lang.RuntimeException:无法创建类fr.djan.fullrecyclerview.Model.NoteViewModel的实例,原因是:java.lang.InstanceionException:java.lang.class没有零参数构造函数
,该构造函数是由AndroidX依赖项中的版本不匹配引起的。参考我按照你的建议和它的工程,谢谢!(即使这不能解决我的问题)尝试
@Query(“从注释表中选择*,如“%”| |;:search | |“%”)LiveData过滤器(字符串搜索)