它使用初始值,但不使用搜索视图文本进行更新:
文件碎片
class TripHome : Fragment() {
private lateinit var mTripViewModel: TripViewModel
override fun onCreateView() {
//trimmed adapter initialize
mTripViewModel = ViewModelProvider(this).get(TripViewModel::class.java)
mTripViewModel.showTripData.observe(viewLifecycleOwner, Observer { trip ->
adapter.setData(trip)
})
view.fab_addTrip.setOnClickListener{
findNavController().navigate(R.id.action_tripHome_to_tripAdd)
}
val menuHost: MenuHost = requireActivity()
val search = menuHost.addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
// Add menu items
menuInflater.inflate(R.menu.searchtrip, menu)
val search = menu.findItem(R.id.searchItems)
val searchView = search.actionView as SearchView
searchView.isSubmitButtonEnabled = true
searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
if (query != null) {
getItemsFromDb(query)
}
return true
}
override fun onQueryTextChange(query: String?): Boolean {
if (query != null) {
getItemsFromDb(query)
}
return true
}
})
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
// Handle the menu selection
return when (menuItem.itemId) {
R.id.searchItems -> {
// clearCompletedTasks()
true
}
else -> false
}
}
}, viewLifecycleOwner, Lifecycle.State.RESUMED)
return view
}
private fun getItemsFromDb(searchText: String) {
var searchText = searchText
searchText = "%$searchText%"
mTripViewModel.searchTrip(query = searchText).observe(this, Observer { trip->
trip?.let {
Log.e("List = ", trip.toString())
}
})
}
}
我的列表适配器文件
class TripList : RecyclerView.Adapter<TripList.MyViewHolder>() {
private var tripList = emptyList<TripModel>()
//trimmed onCreateViewHolder and onBindViewHolder
fun setData(trip: List<TripModel>){
this.tripList = trip
notifyDataSetChanged()
}
}
我测试了搜索视图,并在logcat中获得了这些详细信息,似乎搜索视图工作正常,但视图未过滤
2022-11-06 21:09:05.0909252/com.示例.新一个E/列表=:[行程模型(标识=3,名称=879,车辆=qweqwe,日期=12/12/4321,开始=qewqe,结束=sdadas,风险=fbcxvc,描述=xcvsdfs)]
有人能帮我吗,我需要的recyclerview可以过滤数据,谢谢。
1条答案
按热度按时间hvvq6cgz1#
这是因为您在筛选以下内容后没有更新适配器: