2
\$\begingroup\$

Problem: Search for users by their name using the GitHub API (sorted by the number of followers) and display the results in a RecyclerView.

Implementation: Hitting the GitHub API every time when user type and calling the adapter to show the data.

 searchText.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

            }

            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun afterTextChanged(s: Editable) {

                Log.v("Text is ",s.toString())

                val url="https://api.github.com/search/users?q="+s.toString().trim()+"+sort:followers"
                fetchData(url)
            }
        })




  private fun  fetchData(url: String) {

        val tag_string_req = "string_req"

        val strReq = StringRequest(Request.Method.GET,
                url, Response.Listener<String> {

                        response ->
                                Log.d(TAG, response.toString())

            val builder = GsonBuilder()
            val gson = builder.create()
            var allItems=gson.fromJson(response,AllUsers::class.java)

            Log.v("No of users",allItems.items?.size.toString())

            resultRecyclerView.adapter=adapter(this@MainActivity,allItems.items)

        }, Response.ErrorListener { error ->

            VolleyLog.d(TAG, "Error: " + error.message)

        })

// Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req)
    }

The code is working fine and giving the desired result.

Issues/point that I am thinking:

Here I am creating the new instance every time when user type. I think there should be the better way of doing this.

Edit: I came with this solution

In OnCreate()

 usersData= ArrayList()

        resultRecyclerView.setHasFixedSize(true)
        resultRecyclerView.layoutManager=LinearLayoutManager(this@MainActivity,LinearLayoutManager.VERTICAL,false)
        itemsAdapter=adapter(this@MainActivity,usersData)
        resultRecyclerView.adapter=itemsAdapter

in OnResume:

calling fetchData() as user typing.

private fun  fetchData(url: String) {

        val tag_string_req = "string_req"

        val strReq = StringRequest(Request.Method.GET,
                url, Response.Listener<String> {

                        response ->
                                Log.d(TAG, response.toString())

            val builder = GsonBuilder()
            val gson = builder.create()
            //val allItems=gson.fromJson(response,AllUsers::class.java)

            val data=gson.fromJson(response,AllUsers::class.java)
            usersData=data.items

            Log.v("No of users",usersData?.size.toString())

            resultRecyclerView.removeAllViews()
            itemsAdapter?.updateData(usersData)
            itemsAdapter?.notifyDataSetChanged()

        }, Response.ErrorListener { error ->

            VolleyLog.d(TAG, "Error: " + error.message)

        })

// Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req)
    }

In Adapter:

fun updateData(users: ArrayList<Items>?){
        this.users=users
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.