我想使用 SearchView 搜索房间数据库中的某个元素,但我遇到了一个问题,因为我无法在 RecyclerViewAdapter 中使用 getFilter 因为我有 ViewModel 也许谁知道如何将所有这些元素组合到一个项目中。
\n我搜索一种使用 Transormations.switchMap 的方法。但我无法连接它们。
\n\n\n产品视图模型
\n
class ProductViewModel(application: Application) : AndroidViewModel(application) {\n private val repository: ProductRepository\n\n val allProducts: LiveData<List<ProductEntity>>\n private val searchStringLiveData = MutableLiveData<String>()\n\n init {\n val productDao = ProductsDB.getDatabase(application, viewModelScope).productDao()\n repository = ProductRepository(productDao)\n allProducts = repository.allProducts\n searchStringLiveData.value = ""\n }\n\n fun insert(productEntity: ProductEntity) = viewModelScope.launch {\n repository.insert(productEntity)\n }\n\n\n val products = Transformations.switchMap(searchStringLiveData) { string ->\n repository.getAllListByName(string)\n\n }\n\n fun searchNameChanged(name: String) {\n searchStringLiveData.value = name\n }\n\n\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n产品道 …