我有基本的导航抽屉活动。然后我有列表活动,它从导航抽屉活动扩展而来,因此导航抽屉也显示在此(列表)活动中。 在基础导航绘制活动(在工具栏中)中,我设计了搜索栏
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<com.mancj.materialsearchbar.MaterialSearchBar
android:id="@+id/srchbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.mancj.materialsearchbar.MaterialSearchBar>
</android.support.v7.widget.Toolbar>
我在父级导航抽屉活动中声明了此工具栏, biut在子列表活动中对其进行了初始化
searchBar=findViewById(R.id.foodlist_srchbar)
此搜索栏应加载最新建议,并在其文本字段中输入文本时进行搜索 并在确认搜索后加载带有结果的列表视图
searchBar.setOnSearchActionListener(object : MaterialSearchBar.OnSearchActionListener{
override fun onButtonClicked(buttonCode: Int) {
}
override fun onSearchStateChanged(enabled: Boolean) {
//When searchBar is closed , restore original adapter
if (!enabled){
recycler_food.adapter=adapter
}
}
override fun onSearchConfirmed(text: CharSequence?) {
// When search is finished , show adapter with results
startSSearch(text)
}
})
StartSSearch方法是
private fun startSSearch(text: CharSequence?) {
searchadapter=object : FirebaseRecyclerAdapter<Food, FoodViewHolder>
(
Food::class.java,
R.layout.food_item,
FoodViewHolder::class.java,
database_food.orderByChild("Name").equalTo(text.toString())
){
override fun populateViewHolder(viewHolder: FoodViewHolder?, model: Food?, position: Int) {
viewHolder!!.foodName.setText(model!!.Name)
Picasso.with(baseContext).load(model.Image).into(viewHolder.foodImage)
viewHolder.itemView.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
val foodDeatail = Intent(this@FoodList, FoodDetailActivity::class.java)
foodDeatail.putExtra("FoodId", searchadapter.getRef(position).key)
startActivity(foodDeatail)
}
})
}
因此,这一切通常都不会在复杂的情况下起作用,但是在我目前的情况下(我在父级导航活动中声明了此搜索工具 并在儿童活动中对其进行了礼节性 并在子活动中向该工具写入方法)不起作用,也不显示搜索结果 你能帮我吗?