要在下拉列表中查看搜索结果,您可以使用PopupWindow
获得搜索结果后,我在房间数据库中实现它
从数据库中获取数据
private fun searchCustomer(param: String) {
var listOfCustomer = ArrayList<LocalCustomer>()
localCustomerRepository.searchLocalCustomers(param).observe(this,
Observer<List<LocalCustomer>> { localCustomerList ->
listOfCustomer = localCustomerList as ArrayList
if (listOfCustomer.size > 0) {
val locationAdapter = CustomerAdapter(context, listOfCustomer)
setupPopupWindow(listOfCustomer)
} else {
Utils.showMsg(this, "No result found")
}
})
}
设置弹出窗口
private fun setupPopupWindow(listOfCustomer: ArrayList<LocalCustomer>) {
val inflater = this.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val layout = inflater.inflate(R.layout.spinner_list, null)
val popupWindow =
PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true)
popupWindow.showAsDropDown(svSearch, 0, 0)
val locationAdapter = CustomerAdapter(context, listOfCustomer)
val listView = layout.findViewById(R.id.lvMenu) as ListView
listView.adapter = locationAdapter
listView.onItemClickListener = AdapterView.OnItemClickListener { adapterView,
view, position, id ->
popupWindow.dismiss()
}
}
spinner_list
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/text_vvvvlight_gry"
android:orientation="vertical">
<ListView
android:id="@+id/lvMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="4dp"/>
</LinearLayout>
搜索视图
svSearch.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(p0: String?): Boolean {
return true
}
override fun onQueryTextChange(param: String?): Boolean {
if (param != null && param != "")
searchCustomer(param)
return true
}
})
CustomerAdapter将是一个 baseAdapter 或者您可以使用 ArrayAdapter