Jumpstar
Jumpstar

Reputation: 65

Sort the items of an RecyclerView - Kotlin Code

I have a data class that contains a member called "prio" (example beneath). This "prio" should create the order of how the recyclerView display the items -> "prio" with the number 1 should be displayed on top of the rv, number 2 beneath number 1, and so on.

Additional Information: But the user could decide which number (prio) the item should have. So the "prio" is not fixed.

Data class

data class TaskModel(

val id: Int,
val task: String,
var prio: Int
)

Question: Could you please help me to create a function to sort the "prio" and initialize this function in recyclerView as the order the recyclerView displayed the items?

Attachment:

Adapter:

open class TaskAdapter (private val context: Context, private val list: ArrayList<TaskModel>)
: RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private val mTaskDetails : TaskModel? = null


private var onClickListener: OnClickListener? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    return MyViewHolder(
        LayoutInflater.from(context).inflate(R.layout.activity_item_adapter, parent, false)
    )
}

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

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    val model = list[position]


    if (holder is MyViewHolder) {
        holder.itemView.tv_task_name.text = model.task

        if(model.prio == 1){
            holder.itemView.tv_priority.setBackgroundColor(Color.parseColor("#DF0101"))

        }else if (model.prio == 2){
            holder.itemView.tv_priority.setBackgroundColor(Color.parseColor("#FFFF00"))

        }else if (model.prio == 3){
            holder.itemView.tv_priority.setBackgroundColor(Color.parseColor("#31B404"))
        }

        //holder.itemView.tv_priority.text = model.prio

        holder.itemView.setOnClickListener{
            if(onClickListener != null){
                onClickListener!!.onClick(position,model)
            }
        }
    }


}
private class MyViewHolder(view: View) : RecyclerView.ViewHolder(view)

//RecyclerView Klickbar machen
interface OnClickListener {
    fun onClick(position: Int, model: TaskModel)
}

MainActivity

 private fun setupTaskRecyclerView(tasklist: ArrayList<TaskModel>){
    rv_task_view.layoutManager = LinearLayoutManager(this)
    rv_task_view.setHasFixedSize(true)

    val taskAdapter = TaskAdapter(this, tasklist)
    rv_task_view.adapter = taskAdapter
 }

Upvotes: 4

Views: 7993

Answers (1)

Shalu T D
Shalu T D

Reputation: 4039

You can sort your array before passing to adapter as below:

tasklist.sortBy { it.prio }

Your code will be changed as below:

private fun setupTaskRecyclerView(tasklist: ArrayList<TaskModel>){
    rv_task_view.layoutManager = LinearLayoutManager(this)
    rv_task_view.setHasFixedSize(true)

    tasklist.sortBy { it.prio }
    val taskAdapter = TaskAdapter(this, tasklist)
    rv_task_view.adapter = taskAdapter
}

Upvotes: 12

Related Questions