'Search Row/Record From Android Room database by search view widget, But When I click after search , It's provide incorrect Position
It's Work fine, but when I search something by search view, It's Showing results by search keyword, works fine. when I click the search result's then it's showing incorrect position. Any solution? I am trying this thing since a week.
Main Activity
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 interface
@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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
