deja
deja

Reputation: 506

Android Studio Kotlin RecyclerView problem

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
    }

}

Upvotes: 0

Views: 637

Answers (2)

MojoJojo
MojoJojo

Reputation: 913

Please check the below code and see the comments:

class MainFragment : Fragment() {
    private var _binding: FragmentMainBinding? = null
    private val binding get() = _binding!!
    /*
    * Note: Declaration of RecyclerAdapter
    * */
    private var savedAdapter: RecyclerAdapter? = null
    private val tasksList = mutableListOf<Model>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        _binding = FragmentMainBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
        savedAdapter = RecyclerAdapter()
        binding.tasksRecyclerview.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = savedAdapter
            itemAnimator = DefaultItemAnimator()
        }
        
        /*
        * Note: You can use repeat() to add dummy data in your list
        * */
        repeat(10) {
            tasksList.add(Model().apply {
                setTask("This is a task")
                setStatus(false)
                setId(it + 1)
            })
        }

        /*
        * Note: Use same RecyclerView Adapter object to access method
        * */
        savedAdapter?.setTasks(tasksList)

    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

Also, try creating classes without using pre-defined keywords, it's not a good practice to use only keywords for the user-defined class names. As your Adapter class name is RecyclerAdapter. It should be like TaskListAdatper or TaskListRecyclerAdatper or whatever you want.

Upvotes: 1

TahirRaza
TahirRaza

Reputation: 81

I see that you are calling

RecyclerAdapter().setTasks(tasksList)

whereas your object for recyclerview is adapter , use the same object for calling the method.

Upvotes: 0

Related Questions