Android Studio 通过搜索视图小部件从Android房间数据库搜索行/记录,但当我搜索后点击,它提供不正确的位置

t2a7ltrp  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(103)

它的工作很好,但当我搜索的东西搜索视图,它的搜索关键字显示结果,工作正常.当我点击搜索结果的,然后它显示不正确的位置.任何解决方案?我一直在尝试这件事,因为一个星期.
主要活动

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);
jtjikinw

jtjikinw1#

一开始您要通过Log检查Db中记录'

listLiveData.observe(this, new Observer<List<ModelClass>>() {
    @Override
    public void onChanged(List<ModelClass> modelClasses) {
        myList.addAll(modelClasses);
        noteAdapter.setList(myList);
        recyclerView.setAdapter(noteAdapter);

        Log.e("List = ", list.toString())


    }
});`

在适配器中实现这些代码行

fun getNoteAt(position: Int) = items[position]

override fun onBindViewHolder(holder: CharacterViewHolder, position: Int) =
    holder.bind(items[position])

相关问题