它的工作很好,但当我搜索的东西搜索视图,它的搜索关键字显示结果,工作正常.当我点击搜索结果的,然后它显示不正确的位置.任何解决方案?我一直在尝试这件事,因为一个星期.
主要活动
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
final List<ModelClass>filteredList = new ArrayList<>();
@Override
public boolean onQueryTextSubmit(String query) {
if (query!=null){
getItemFromDb(query);
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText!=null){
getItemFromDb(newText);
}else {
//when search view is empty then set all data to the adapter class
noteAdapter.setList(listData);
}
return true;
}
});
private void getItemFromDb(String query){
String searchText = "%"+query+"%";
final List<ModelClass>myList = new ArrayList<>(); //now it is empty
LiveData<List<ModelClass>>listLiveData = viewModel.getSearchData(searchText);
listLiveData.observe(this, new Observer<List<ModelClass>>() {
@Override
public void onChanged(List<ModelClass> modelClasses) {
myList.addAll(modelClasses);
noteAdapter.setList(myList);
recyclerView.setAdapter(noteAdapter);
}
});
}
刀接口
@Dao
public interface NoteDao {
@Insert
void insert(ModelClass modelClass);
@Delete
void delete(ModelClass modelClass);
@Update
void update(ModelClass modelClass);
@Query("SELECT * FROM myNewNote")
LiveData<List<ModelClass>>getAllNote();
@Query("SELECT * FROM myNewNote WHERE title LIKE ( :searchQuery) ORDER BY id")
LiveData<List<ModelClass>> getSearchDatabase(final String searchQuery);
1条答案
按热度按时间jtjikinw1#
一开始您要通过Log检查Db中记录'
在适配器中实现这些代码行