我在 Fragment 中使用 Searchview,我的数据没有添加到我的新数组中使用 Kotlin
I am using Searchview in Fragment and my data is not added in my New array Using Kotlin
我在 Fragments 中添加 Searchview 并且 Menuitem 是 Inflated 但是当我点击 Searchview 按钮并输入查询时 Nothing Changes 并且我的数据不是 added.please 帮助
这是代码
答案应该是 Kotlin
覆盖乐趣 onQueryTextChange(p0: String?): 布尔值 {
temp.clear() //this is my temp array
var text=p0?.lowercase(Locale.getDefault())
if (text!!.isNotEmpty()){
makeText(activity as Context,"working",Toast.LENGTH_LONG).show()
value.forEach {
temp.add(it) //by this line data shoul add but not added
}
}else{
makeText(activity as Context,"no",Toast.LENGTH_LONG).show()
temp.clear()
temp.addAll(value)
recyclerView.adapter?.notifyDataSetChanged()
}
return true
}
})
您应该检查用户的输入文本是否不为空,更新您的适配器数据,然后调用 notifyDataSetChanged 来刷新您的列表。
将您的第一个 if 范围更改为此:
if (text!!.isNotEmpty()) {
makeText(activity as Context, "working", Toast.LENGTH_LONG).show()
value.forEach {
temp.add(it) //by this line data shoul add but not added
}
recyclerView.adapter?.clear()
recyclerView.adapter?.addAll(temp)
recyclerView.adapter?.notifyDataSetChanged()
}
我在 Fragments 中添加 Searchview 并且 Menuitem 是 Inflated 但是当我点击 Searchview 按钮并输入查询时 Nothing Changes 并且我的数据不是 added.please 帮助
这是代码 答案应该是 Kotlin
覆盖乐趣 onQueryTextChange(p0: String?): 布尔值 {
temp.clear() //this is my temp array
var text=p0?.lowercase(Locale.getDefault())
if (text!!.isNotEmpty()){
makeText(activity as Context,"working",Toast.LENGTH_LONG).show()
value.forEach {
temp.add(it) //by this line data shoul add but not added
}
}else{
makeText(activity as Context,"no",Toast.LENGTH_LONG).show()
temp.clear()
temp.addAll(value)
recyclerView.adapter?.notifyDataSetChanged()
}
return true
}
})
您应该检查用户的输入文本是否不为空,更新您的适配器数据,然后调用 notifyDataSetChanged 来刷新您的列表。
将您的第一个 if 范围更改为此:
if (text!!.isNotEmpty()) {
makeText(activity as Context, "working", Toast.LENGTH_LONG).show()
value.forEach {
temp.add(it) //by this line data shoul add but not added
}
recyclerView.adapter?.clear()
recyclerView.adapter?.addAll(temp)
recyclerView.adapter?.notifyDataSetChanged()
}