skip to Main Content

Here is my Fragment1.kt overall code

class Fragment1 : Fragment() {
    private lateinit var binding: Fragment1Binding
    private lateinit var list : ArrayList<TasksItem>
    private lateinit var recyclerView: RecyclerView
    private lateinit var manager: RecyclerView.LayoutManager
    private lateinit var myAdapter: RecyclerView.Adapter<*>
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_1, container, false)

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        recyclerView = view.findViewById(R.id.app_list)
        recyclerView.setHasFixedSize(true)

        list = arrayListOf()
        manager = LinearLayoutManager(this.requireContext())

        GroomedKidzApi.retrofitService.getTasksProgress().enqueue(object :
            Callback<List<TasksItem>?> {
            override fun onResponse(
                call: Call<List<TasksItem>?>,
                response: Response<List<TasksItem>?>
            ) {
                val responseBody = response.body()

                if (responseBody != null) {
                    recyclerView = view.findViewById<RecyclerView>(R.id.app_list).apply {
                        myAdapter = TaskAdapter(response.body()!!)
                        layoutManager = manager
                        adapter = myAdapter
                        Log.e("TAG", "response 33: "+ (response.body()) )
                    }
                }
            }

            override fun onFailure(call: Call<List<TasksItem>?>, t: Throwable) {

            }
        })


        var adapter = TaskAdapter(list)
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(object : TaskAdapter.OnItemClickListener {
            override fun onItemClick(position: Int) {
                    val intent = Intent(activity,ViewTaskActivity::class.java).apply{
                        putExtra("taskname", list[position].taskName)
                        putExtra("desc", list[position].taskDesc)
                        putExtra("category", list[position].category)
                        putExtra("duedate", list[position].dueDate)
                        putExtra("assignee", list[position].assignee)
                        putExtra("point", list[position].pointEarned)
                        Log.d("Fragment1", "onViewCreated executed")
                }
                startActivity(intent)
            }
        })
    }
}

Here is the TaskAdapter overall code

class TaskAdapter(val list: List<TasksItem> ) : RecyclerView.Adapter<TaskAdapter.MyViewHolder>() {

    private var listener2: OnItemClickListener? = null

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(listener: OnItemClickListener){
        listener2 = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item,
            parent,false)
        return MyViewHolder(itemView,listener2)
    }


    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

        val currentitem = list[position]

        holder.TaskName.text = currentitem.taskName
        holder.Date.text = currentitem.dueDate
        holder.Assignee.text = currentitem.assignee
        holder.Status.text = currentitem.taskStatus

    }

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


    class MyViewHolder(itemView : View, listener: OnItemClickListener?) : RecyclerView.ViewHolder(itemView){

        val TaskName : TextView = itemView.findViewById(R.id.tvTaskname)
        val Assignee : TextView = itemView.findViewById(R.id.tvAssignee)
        val Date : TextView = itemView.findViewById(R.id.tvDate)
        val Status : TextView = itemView.findViewById(R.id.tvStatus)

        init {
            itemView.setOnClickListener{
                listener?.onItemClick(adapterPosition)
            }
        }
    }
}

The logcat for Log.d("Fragment1", "onViewCreated executed") inside the onItemClick is not being called. However, others functions can be called successfully.

Any reason why is the onItemClick is not being called? All functions works fine even the API.

Notes: Adapter is fine, setOnClickListener able to get the position as well

2

Answers


  1. It seems like there might be an issue with the setOnClickListener implementation in your TaskAdapter.

    class TaskAdapter(val list: List<TasksItem>) : RecyclerView.Adapter<TaskAdapter.MyViewHolder>() {
    
        private var listener: OnItemClickListener? = null
    
        interface OnItemClickListener {
            fun onItemClick(position: Int)
        }
    
        fun setOnItemClickListener(listener: OnItemClickListener) {
            this.listener = listener
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
            val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
            return MyViewHolder(itemView, listener)
        }
    
        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
            val currentitem = list[position]
            holder.TaskName.text = currentitem.taskName
            holder.Date.text = currentitem.dueDate
            holder.Assignee.text = currentitem.assignee
            holder.Status.text = currentitem.taskStatus
        }
    
        override fun getItemCount(): Int {
            return list.size
        }
    
        class MyViewHolder(itemView: View, listener: OnItemClickListener?) :
            RecyclerView.ViewHolder(itemView) {
    
            val TaskName: TextView = itemView.findViewById(R.id.tvTaskname)
            val Assignee: TextView = itemView.findViewById(R.id.tvAssignee)
            val Date: TextView = itemView.findViewById(R.id.tvDate)
            val Status: TextView = itemView.findViewById(R.id.tvStatus)
    
            init {
                itemView.setOnClickListener {
                    listener?.onItemClick(adapterPosition)
                }
            }
        }
    }
    
    

    Ensure that you are calling setOnItemClickListener on your adapter instance in the Fragment1 class:

    adapter.setOnItemClickListener(object : TaskAdapter.OnItemClickListener {
        override fun onItemClick(position: Int) {
            val intent = Intent(activity, ViewTaskActivity::class.java).apply {
                // Put extras
            }
            startActivity(intent)
        }
    })
    
    Login or Signup to reply.
  2. I think you must call setOnClickListener in onBindViewHolder

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        ...
        holder.ItemView.setOnClickListener{
            listener2?.onItemClick(position)
        }
    }
    
    class MyViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView){
        val ItemView : View = itemView
        ...
    }
    

    and please note that recyclerView.adapter is re-assigned to myAdapter that has no click listener

    myAdapter = TaskAdapter(response.body()!!)
    layoutManager = manager
    adapter = myAdapter
    

    You can refer this way:

        if (responseBody != null) {
            myAdapter.updateList(response.body()!!)
        }
                
    myAdapter = TaskAdapter(list)
    recyclerView.adapter = myAdapter
    myAdapter.setOnItemClickListener(...)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search