I implemented the searchview with a RecyclerView in a Fragment.The problem is, when I press search and I type the product, there are no results. I am dealing with this problem for more than 2 weeks, after trying almost everything found. This is my code for the Search Fragment:
class SearchFragment : Fragment(R.layout.fragment_search) {
val s = ArrayList<NewShoe>()
val displays=ArrayList<NewShoe>()
lateinit var adapter:AdapterSearch
var sp=ArrayList<Spec>()
override fun onCreate(savedInstanceState: Bundle?) {
setHasOptionsMenu(true)
super.onCreate(savedInstanceState)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val recyclerView= view?.findViewById<RecyclerView>(R.id.recyclerViewSearch)
recyclerView.layoutManager= GridLayoutManager(this.context?.applicationContext,2)
recyclerView.isNestedScrollingEnabled=false
recyclerView.setHasFixedSize(true)
searchView.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
println("here1")
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
adapter.filter.filter(newText)
println("here2")
println(adapter.filter.filter(newText).toString())
return true
}
})
addList()
super.onViewCreated(view, savedInstanceState)
}
fun addList()
{
sp.add(Spec(R.drawable.shield,"Anti-pollution, anti-dust"))
sp.add(Spec(R.drawable.crossing,"Reusable"))
sp.add(Spec(R.drawable.happy_face,"Pleated at sides for extra comfort"))
sp.add(Spec(R.drawable.sun,"Wider face coverage for maximum \n" + "protection"))
s.add(
NewShoe(
R.drawable.air_max_london, "Nike air max 1", "LONDON",289,"",
arrayListOf(39,40,41), arrayListOf(
Spec(0,"Anti-pollution, anti-dust"),
Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
"protection ")
))
)
s.add(
NewShoe(
R.drawable.air_max_1_have_a_nikeday, "Nike air max 1", "Have A Nike Day",149,"",
arrayListOf(39,40,41), arrayListOf(
Spec(0,"Anti-pollution, anti-dust"),
Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
"protection ")
))
)
s.add(
NewShoe(
R.drawable.limeade, "Nike air max 1", "Limeade",209,"",
arrayListOf(39,40,41), arrayListOf(
Spec(0,"Anti-pollution, anti-dust"),
Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
"protection ")
))
)
s.add(
NewShoe(
R.drawable.jordan_canyon, "Jordan 1 mid", "\" Canyon Rust\"", 230,"",
arrayListOf(39,40,41), arrayListOf(
Spec(0,"Anti-pollution, anti-dust"),
Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
"protection ")
))
)
s.add(
NewShoe(
R.drawable.jordan_particle, "Jordan 1 mid", "SE Particle Beige", 210,"",
arrayListOf(39,40,41), arrayListOf(
Spec(0,"Anti-pollution, anti-dust"),
Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
"protection ")
))
)
s.add(
NewShoe(
R.drawable.jordan_yellow, "Jordan 1 mid", "SE Voltage Yellow", 170,"",
arrayListOf(39,40,41), arrayListOf(
Spec(0,"Anti-pollution, anti-dust"),
Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
"protection ")
))
)
s.add(
NewShoe(
R.drawable.airmax_90_crock, "Nike airmax 90", "Croc", 190,"",
arrayListOf(39,40,41), arrayListOf(
Spec(0,"Anti-pollution, anti-dust"),
Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
"protection ")
))
)
s.add(
NewShoe(
R.drawable.dance_floor_green, "Nike airmax 90", "90S Dancefloor Green", 190,"",
arrayListOf(39,40,41), arrayListOf(
Spec(0,"Anti-pollution, anti-dust"),
Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
"protection ")
))
)
s.add(
NewShoe(
R.drawable.duck_camo, "Nike airmax 90", "Duck Camo Orange", 140,"",
arrayListOf(39,40,41), arrayListOf(
Spec(0,"Anti-pollution, anti-dust"),
Spec(0,"Reusable"), Spec(0,"Pleated at sides for extra comfort"), Spec(0,"Wider face coverage for maximum \n" +
"protection ")
))
)
displays.addAll(s)
val recyclerView= view?.findViewById<RecyclerView>(R.id.recyclerViewSearch)
recyclerView?.adapter= AdapterSearch(s,object: AdapterSearch.OnClickListener{
override fun onItemClick(position: Int) {
val i= Intent(activity,BuyingProducts::class.java)
i.putExtra("price",s[position].price)
i.putExtra("name",s[position].name)
i.putExtra("model",s[position].model)
i.putExtra("sizes",s[position].sizes)
i.putExtra("picture",s[position].image)
i.putExtra("spec",sp)
startActivity(i)
}
})
}
}
In the Adapter I implemented the Filter method,creating a new arraylist. The code for the Adapter:
class AdapterSearch(private var li:ArrayList<NewShoe>, val onClickListener:OnClickListener):
RecyclerView.Adapter<RecyclerView.ViewHolder>(),Filterable
{
var search=ArrayList<NewShoe>()
lateinit var mcontext: Context
interface OnClickListener
{
fun onItemClick(position:Int)
}
class ShoeHolder(itemView: View): RecyclerView.ViewHolder(itemView)
init {
search=li
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val searchView= LayoutInflater.from(parent.context.applicationContext).inflate(R.layout.card_jordan,parent,false)
val sch = ShoeHolder(searchView)
mcontext = parent.context
return sch
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
holder.apply {
search[position].image?.let {
itemView.findViewById<ImageView>(R.id.jordan_image).setImageResource(it)
}
itemView.findViewById<TextView>(R.id.jordan_name).text=search[position].name
itemView.findViewById<TextView>(R.id.jordan_model).text=search[position].model
itemView.findViewById<TextView>(R.id.price_jordan).text=search[position].price.toString()
}
holder.itemView.setOnClickListener {
onClickListener.onItemClick(position)
}
}
override fun getItemCount(): Int {
return search.size
}
override fun getFilter(): Filter {
return object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val queryString = constraint.toString()
if(queryString.isEmpty())
search=li
else {
val resultList=ArrayList<NewShoe>()
for(it in li)
{
if(it.name.toLowerCase(Locale.ROOT)
.contains(queryString.toLowerCase()) || it.price.toString()
.contains(queryString.toLowerCase()) ||
it.model.toLowerCase(Locale.ROOT)
.contains(queryString.toLowerCase()))
resultList.add(it)
}
search=resultList
}
val filterResults = FilterResults()
filterResults.values = search
return filterResults
}
override fun publishResults(charSequence: CharSequence?, filterResults: FilterResults) {
search = filterResults.values as ArrayList<NewShoe>
notifyDataSetChanged()
}
}
}
}