0

i have a fragment that have a recyclerview i'm trying to use setOnQueryTextListener to search some specific data and display the new data instead of the old one

this is my Fragmenttask.kt file

enter image description here

class Fragmenttask : Fragment(), SwipeRefreshLayout.OnRefreshListener {

    private var mListener: OnFragmentInteractionListener? = null
    private var mSwipeRefreshLayout: SwipeRefreshLayout? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        //container!!.removeAllViews()
        setHasOptionsMenu(true)
        val CustumViewtask = inflater!!.inflate(R.layout.fragment_fragmenttask, container, false)
        val taskMainRV = CustumViewtask.findViewById(R.id.recyclerView_main) as RecyclerView
        //taskMainRV.setBackgroundColor(Color.BLUE)

        taskMainRV.layoutManager = LinearLayoutManager(context)
        //recyclerView_main.adapter = MainAdapter()

        fetchJson(taskMainRV)

        return CustumViewtask
    }

    override fun onRefresh() {
        Toast.makeText(context, "تم التحميل بنجاح والحمد لله", Toast.LENGTH_LONG).show()
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.menu_user, menu)
        val searchItem = menu.findItem(R.id.action_search)
        val searchView = MenuItemCompat.getActionView(searchItem) as SearchView
        searchView.queryHint = "أكتب كلمة بحث ..."

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextChange(q: String): Boolean {
                println("my serch : $q")
                //fetchJson(taskMainRV)
                //adaptertask?.notifyDataSetChanged()
                //notifyDataSetChanged
                println("mohamed 78797987897987987987987987987987987987897")
                return false
            }

            override fun onQueryTextSubmit(q: String): Boolean {
                println("my serch : $q")

                println("mohamed 78797987897987987987987987987987987987897**************")
                return false
            }
        })
        //searchView.setOnQueryTextListener(this)
        //https://stackoverflow.com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
    }

    fun onQueryTextChange(query: String): Boolean {
        // Here is where we are going to implement the filter logic
        return false
    }

    fun onQueryTextSubmit(query: String): Boolean {
        return false
    }

    fun fetchJson(RSV: RecyclerView) {

        //SharedPreferences
        val MY_APP_INFO: String = "UserInfo"
        val prefs = activity.getSharedPreferences(MY_APP_INFO, AppCompatActivity.MODE_PRIVATE)
        val LoggedUserId = prefs.getString("UserId", null)
        var serchqury = arguments.getString("serchqury")

        if(serchqury.isNullOrEmpty()){
            serchqury = ""
        }
        println(serchqury+" ------------**/*/*/*/*/*/*/*/*/*/ ")
        println("your code is : $LoggedUserId")

        println("Attempting to Fetch JSON")

        val url = "http://www.deraah-rs.com/1mahamatonline/html/Restful/get_tasks.php"

        val client = OkHttpClient()

        val formBody = FormBody.Builder().add("UserId", LoggedUserId)
                                                    .add("serchqury", serchqury).build()

        val request = Request.Builder().url(url)
                .post(formBody)
                .build()

        client.newCall(request).enqueue(object: Callback {
            override fun onResponse(call: Call?, response: Response?) {
                val body = response?.body()?.string()
                println("mohamed : $body")

                val gson = GsonBuilder().create()

                val tasksfeed = gson.fromJson(body, M_tasksFeed::class.java)

                activity.runOnUiThread {
                    RSV.adapter = MainAdaptertasks(tasksfeed)
                    if(taskfragmentprogressbar != null){
                        taskfragmentprogressbar.visibility = View.INVISIBLE
                    }
                    //ChatAdapter(Context, tasksfeed)
                }

            }

            override fun onFailure(call: Call?, e: IOException?) {
                println("Failed to execute request")
            }
        })
    }

    // TODO: Rename method, update argument and hook method into UI event
    fun onButtonPressed(uri: Uri) {
        if (mListener != null) {
            mListener!!.onFragmentInteraction(uri)
        }
    }



    companion object {
        fun newInstance(): Fragmenttask {
            val fragment = Fragmenttask()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }
}// Required empty public constructor

......................................................................................................................................

Benkerroum Mohamed
  • 1,867
  • 3
  • 13
  • 19
  • Your code is quite unreadable . Here is two things you can do if search is performing locally on data then you should use [Filterable](https://developer.android.com/reference/android/widget/Filterable). And if you have to hit a service on text change for search then use a `Handler` with some seconds delay to hit API . Or you can use `debounce` operator if you are using `RX`. – ADM Jul 19 '18 at 08:03
  • 1
    *to search some specific data and display the new data instead of the old one*. Be more specific/ –  Jul 19 '18 at 08:06
  • You can set adapter with new data (as per the search keyword) inside onQueryTextChange(). If keyword is empty, set original data – Harin Jul 19 '18 at 11:16

0 Answers0