How to fix SearchView not displaying result in Fragment Kotlin

Viewed 20

Please, I'm trying to implement SearchView on a Fragment which is already fetching list from firestore,whenever i type a text into the searchview it notifies the adapter but doesnt return result at all, the Fragment becomes empty till i refresh the fragment then the recyclerview items will return with no changes. I have tried so many solutions found here and other platforms but nothing is working. Thank you in advance.

Below is my Fragment

class PersonalFragment : BaseFragment(), SearchView.OnQueryTextListener {

    val list = ArrayList<PersonalCategory>()
    val displayList = ArrayList<PersonalCategory>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // If we want to use the option menu in fragment we need to add it.
        setHasOptionsMenu(true)


    }

    // END
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_personal, container, false)

        return root
    }


    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.create_personal_cat_top_menu, menu)
        val ic_search = menu!!.findItem(R.id.action_search_personal_cat)
        val searchView = ic_search.actionView as SearchView
        searchView.setOnQueryTextListener(this)
        super.onCreateOptionsMenu(menu, inflater)
    }


    override fun onQueryTextSubmit(filterString: String): Boolean {
        // search button submit
        return true
    }

    override fun onQueryTextChange(newText: String): Boolean {
        // search text changed
        if (!newText.isNullOrEmpty()) {
            displayList.clear()
            val search = newText.lowercase(Locale.getDefault())


            val filteredList = list.filter{ it.personal_category_title.lowercase(Locale.getDefault()).contains(search) }

            (rv_my_personalCate_items.adapter as? MyPersonalCategoryListAdapter)?.refreshList(
                filteredList as ArrayList<PersonalCategory>
            )

        }
        else{
            displayList.clear()
            displayList.addAll(list)
            (rv_my_personalCate_items.adapter as? MyPersonalCategoryListAdapter)?.refreshList(displayList)

        }

        return true
    }



    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId

        when (id) {
            R.id.action_create_personal_cat -> {

                // Launch the Create Personal Category Activity on click of action item.
                // START
                startActivity(Intent(activity, CreatePersonalCategoryActivity::class.java))
                // END
                return true
            }


        }
        return super.onOptionsItemSelected(item)
    }
    // END


    override fun onResume() {
        super.onResume()
        getPersonalListFromFireStore()
    }

    //Getting the list by call the  firestore class
    private fun getPersonalListFromFireStore() {
        // Show the progress dialog.
        showProgressDialog(resources.getString(R.string.please_wait))

        // Call the function of Firestore class.
        FirestoreClass().getPersonalCategoryList(this@PersonalFragment)
    }

    /**
     * A function to get the successful PERSONAL CATEGORY list from cloud firestore.
     *
     * @param pcategoryList Will receive the academic category from cloud firestore.
     */
    fun successPersonalListFromFireStore(pcategoryList: ArrayList<PersonalCategory>) {

        // Hide Progress dialog.
        hideProgressDialog()
        if (pcategoryList.size > 0) {
            rv_my_personalCate_items.visibility = View.VISIBLE
            tv_no_personalCate_found.visibility = View.GONE

            rv_my_personalCate_items.layoutManager = LinearLayoutManager(activity)
            rv_my_personalCate_items.setHasFixedSize(true)


            val adapterPersonalCates =
                MyPersonalCategoryListAdapter(
                    requireActivity(),
                    pcategoryList,
                    this@PersonalFragment
                )
            // END
            rv_my_personalCate_items.adapter = adapterPersonalCates
}
            else {
            rv_my_personalCate_items.visibility = View.GONE
            tv_no_personalCate_found.visibility = View.VISIBLE
        }

    }

    //END
}

Below is my Adapter

open class MyPersonalCategoryListAdapter(
    private val context: Context,
    private var list: ArrayList<PersonalCategory>,
    private val fragment: PersonalFragment
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return MyViewHolder(
            LayoutInflater.from(context).inflate(
                R.layout.item_list_personal_categories_layout,
                parent,
                false
            )
        )
    }


 
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val model = list[position]



        if (holder is MyViewHolder) {


            holder.itemView.tv_perscate_name.text = model.personal_category_title
            // END
            holder.itemView.setOnClickListener {
                // Launch Academic Task Screen details screen.
                val intent = Intent(context, PersonalTaskActivity::class.java)
                intent.putExtra(Constants.EXTRA_PERSONAL_CATEGOTY_ID, model.personal_category_id)
                context.startActivity(intent)
            }
        }
    }


 
    override fun getItemCount(): Int {
        return list.size

    }
    fun refreshList(displayList:ArrayList<PersonalCategory>){
        this.list.clear()
        this.list.addAll(displayList)
        notifyDataSetChanged()
    }


   
    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view)


}

Below is SearchView XML

<item
        android:id="@+id/action_search_personal_cat"
        android:icon="@drawable/ic_search"
        android:title="@string/action_search_academic"
        app:showAsAction="always"
        app:actionViewClass="androidx.appcompat.widget.SearchView"/>
    

Below is Logcat result

2022-09-18 19:41:13.765 16034-16034/com.catetaskv2 W/StaticLayout: maxLineHeight should not be -1.  maxLines:1 lineCount:1
2022-09-18 19:41:13.766 16034-16034/com.catetaskv2 I/chatty: uid=10088(com.catetaskv2) identical 2 lines
2022-09-18 19:41:13.766 16034-16034/com.catetaskv2 W/StaticLayout: maxLineHeight should not be -1.  maxLines:1 lineCount:1
2022-09-18 19:41:13.769 16034-16034/com.catetaskv2 W/StaticLayout: maxLineHeight should not be -1.  maxLines:1 lineCount:1
2022-09-18 19:41:13.773 16034-16034/com.catetaskv2 I/chatty: uid=10088(com.catetaskv2) identical 6 lines
2022-09-18 19:41:13.773 16034-16034/com.catetaskv2 W/StaticLayout: maxLineHeight should not be -1.  maxLines:1 lineCount:1
2022-09-18 19:41:13.813 16034-16034/com.catetaskv2 W/StaticLayout: maxLineHeight should not be -1.  maxLines:1 lineCount:1
2022-09-18 19:41:13.819 16034-16034/com.catetaskv2 I/chatty: uid=10088(com.catetaskv2) identical 10 lines
2022-09-18 19:41:13.820 16034-16034/com.catetaskv2 W/StaticLayout: maxLineHeight should not be -1.  maxLines:1 lineCount:1
0 Answers
Related