Home > Mobile >  I am using Searchview in Fragment and my data is not added in my New array Using Kotlin
I am using Searchview in Fragment and my data is not added in my New array Using Kotlin

Time:01-19

I am adding Searchview in Fragments and Menuitem is Inflated but when I click on Searchview Button and enter query Nothing Changes and my Data is not added.please help

here is the code Answer should be in Kotlin

override fun onQueryTextChange(p0: String?): Boolean {

            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
        }
    })

CodePudding user response:

You should check if user's input text is not empty, update data of your adapter and then call notifyDataSetChanged to refresh your list.

change your first if scope to this:

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()
}
  •  Tags:  
  • Related