Why is my recyclerview not showing on my tab layout fragment?

Viewed 202

So I'm trying to display the following and followers recyclerview from GitHub's API. So there are two fragments for 2 tablayouts for followers and following.

This is the code for my FollowingFragment

 class FollowingFragment : Fragment() {

    private lateinit var binding: FragmentFollowingBinding
    private var list = ArrayList<Users>()

    companion object {
        private val ARG_USERNAME = "extra_username"

        fun newInstance(username:String):Fragment{
            var fragment = FollowingFragment()
            val bundle = Bundle()
            bundle.putString(ARG_USERNAME,username)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val username = arguments?.getString(ARG_USERNAME)
        setListUser(username)
        binding.rvFollowing.setHasFixedSize(true)
        showRecyclerList()
    }

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

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        binding = FragmentFollowingBinding.inflate(inflater,container,false)
        val view: View = binding.root
        return view
    }

    private fun showRecyclerList() {
        binding.rvFollowing.layoutManager = LinearLayoutManager(activity)
        val listUserAdapter = FollowingAdapter(list)
        binding.rvFollowing.adapter = listUserAdapter
    }

    private fun setListUser(username: String?) {
        val listItems = ArrayList<Users>()
        val token = "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"
        val url = "https://api.github.com/users/$username/following"

        val client = AsyncHttpClient()
        client.addHeader("Authorization","token $token")
        client.addHeader("User-Agent","request")
        client.get(url,object: AsyncHttpResponseHandler(){
            override fun onSuccess(statusCode: Int, headers: Array<Header>, responseBody: ByteArray) {
                //parsingjson
                try {
                    val result = String(responseBody)
                    val items = JSONArray(result)

                    for (i in 0 until items.length()){
                        val UserClass = Users()
                        val user = items.getJSONObject(i)
                        val username = user.getString("login")
                        val avatar = user.getString("avatar_url")
                        UserClass.login = username
                        UserClass.avatar = avatar
                        listItems.add(UserClass)
                        Log.d("TAG","$username $avatar")
                    }
                    list = listItems
                } catch (e: Exception){
                    Log.d("Exception",e.message.toString())
                }
            }

            override fun onFailure(statusCode: Int, headers: Array<Header>, responseBody: ByteArray, error: Throwable) {
                Log.d("onFailure",error.message.toString())
            }

        })
    }
}

This is the code for my following recyclerview adapter:

    class FollowingAdapter(private val listFollowing: ArrayList<Users>): RecyclerView.Adapter<FollowingAdapter.UserViewHolder>() {
    class UserViewHolder(private val binding:ItemRowDeveloperProfilesBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(user:Users){
            with(binding){
                Glide.with(itemView.context)
                        .load(user.avatar)
                        .apply(RequestOptions().override(400,400))
                        .into(imgProfilePictureSearch)
                usernameSearch.text = user.login
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
        val binding = ItemRowDeveloperProfilesBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return UserViewHolder(binding)
    }

    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        holder.bind(listFollowing[position])
    }

    override fun getItemCount(): Int {
        return listFollowing.size
    }


}

The full code can be seen on my github repository https://github.com/briancatraguna/GitHubClone. I'm really not sure what I did wrong here? Can anyone please help me?

1 Answers

You are setting the listItems before getting the list from the api calls ( this is true for your search users page as well).

Move your showRecyclerView() to the onSuccess callback of the AsyncHttpClient get Call ..

Related