I have a problem with displaying my RecyclerView
. As far as i can see it seems like i didn’t ommit any important part of the setup. Could you help me with finding the issue?
Model
– the class where i retrieve and store all the data, such a id, status, task
class Model() {
private var id: Int = -1
private var status: Boolean = false
private var task: String = ""
fun getId(): Int {
return id
}
fun setId(id: Int){
this.id = id
}
fun getStatus(): Boolean {
return status
}
fun setStatus(status: Boolean) {
this.status = status
}
fun getTask(): String {
return task
}
fun setTask(task: String) {
this.task = task
Adapter
– here is an adapter of the RecyclerView
. I think there’s an issue with updating the informations. The function which is responsible for that is setTasks()
class RecyclerAdapter : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
var data = listOf<Model>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.task_layout, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = data[position]
holder.task.text = item.getTask()
holder.task.isChecked = item.getStatus()
}
override fun getItemCount() = data.size
fun setTasks(data: List<Model>) {
this.data = data
notifyDataSetChanged()
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val task : CheckBox = itemView.findViewById(R.id.checkbox_task)
}
}
MainFragment
– here is the fragment on which i want to display the RecyclerView
. In the bottom i added some simple "hard" tasks, just to check if everything is displaying properly
class MainFragment : Fragment() {
private var _binding: FragmentMainBinding? = null
private val binding get() = _binding!!
private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter: RecyclerView.Adapter<RecyclerAdapter.ViewHolder>? = null
private val tasksList = mutableListOf<Model>()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View? {
_binding = FragmentMainBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.tasksRecyclerview.apply {
layoutManager = LinearLayoutManager(context)
adapter = RecyclerAdapter()
itemAnimator = DefaultItemAnimator()
}
val task = Model()
task.setTask("This is a task")
task.setStatus(false)
task.setId(1)
tasksList.add(task)
tasksList.add(task)
tasksList.add(task)
tasksList.add(task)
tasksList.add(task)
RecyclerAdapter().setTasks(tasksList)
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
2
Answers
I see that you are calling
whereas your object for recyclerview is
adapter
, use the same object for calling the method.Please check the below code and see the comments:
Also, try creating classes without using pre-defined
keywords
, it’s not a good practice to use onlykeywords
for the user-defined class names. As yourAdapter
class name isRecyclerAdapter
. It should be likeTaskListAdatper
orTaskListRecyclerAdatper
or whatever you want.