Android 在数据库中搜索项目并显示下拉列表
我有以下应用程序架构(为了简化而通用): 主Android 在数据库中搜索项目并显示下拉列表,android,android-sqlite,android-search,Android,Android Sqlite,Android Search,我有以下应用程序架构(为了简化而通用): 主布局包括带有自定义视图和可滚动的选项卡的操作栏,以及承载多个列表片段的查看页面。每个ListFragment都有自己的一组数据(1行=ImageView+TextView),这些数据是从SQLite数据库加载的。在我的ActionBar的自定义视图中a有一个“搜索”EditText 当用户开始键入某些内容时,我希望在数据库中执行“在路上”搜索,并以下拉列表的形式显示匹配的名称(这些名称应该可以单击)。我看到了一些如何在列表视图中执行搜索的示例,但与数据
布局
包括带有自定义视图
和可滚动的选项卡的操作栏
,以及承载多个列表片段的查看页面
。每个ListFragment
都有自己的一组数据(1行=ImageView
+TextView
),这些数据是从SQLite数据库加载的。在我的ActionBar的
自定义视图中
a有一个“搜索”EditText
当用户开始键入某些内容时,我希望在数据库中执行“在路上”搜索,并以下拉列表的形式显示匹配的名称(这些名称应该可以单击)。我看到了一些如何在列表视图中执行搜索的示例,但与数据库搜索无关
所以问题是:如何将数据库搜索结果显示为下拉列表,并在用户添加或删除字符时刷新它?您需要实现。这允许在用户键入时提供建议。您可以使用绑定要搜索的数据
这应该能解决你的问题
可以找到一个可能有用的示例。您可以实现onquerytextliner()
接口并重写onQueryTextChange(字符串文本)
方法-此方法在字符更改时被调用。差不多
searchTextInstance.setOnQueryTextListener(new CustomQueryListener());
在onQueryTextChange(Sting text)
中查询您的数据库,然后调用
CustomAdapter adapter = new CustomAdapter()
//... populate the adapter with the query results and call
searchTextInstance.setSuggestionAdapter(adapter)
其中CustomAdapter是一个扩展SimpleCrsorAdapter的类,此实例由查询结果(将在下拉菜单中显示的结果)填充
要使选择可单击,您可以将自定义OnSuggestionListener()设置为您的searchTextInstance
,即
searchTextInstance.setOnSuggestionListener(new CustomSuggestionListener());
如果CustomSuggestionListener
实现了界面的唯一方法OnSuggestionListener
,onSuggestionClick(int position)
将实现您的目标在下拉列表中查看搜索结果,您可以使用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")
}
})
}
CustomerAdapter将是baseAdapter,或者您可以使用ArrayAdapter
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()
}
}
<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
}
})