I'm making an Android app in Android Studio for my girlfriend to help her keep track of her tasks at work. The app has multiple to-do lists using RecyclerView. Since each checklist has their own activity, and she will be switching between them, how do I save which boxes have been checked so that they remained checked when switching between activities? Here is the RecyclerView adapter code for one of the to-do lists that also contains the Checkbox code.
package com.mattkalichman.coffee
import android.util.SparseBooleanArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.checkbox_row.view.*
class ThreeHourPullAdapter(var list: ArrayList<Data>) :
RecyclerView.Adapter<ThreeHourPullAdapter.ViewHolder>() {
private var titles =
arrayOf(
"Log into the handheld under Pull to Thaw.",
"Count anything left on pastry cart (BOH).",
"Count everything on front pastry cart (FOH).",
"Put remaining pastries from BOH on FOH cart. Remember to FIFO!",
"Pull from freezer to BOH cart. Adjust number of pulled items according to inventory.",
"When pull is done, press complete pull at the bottom of the screen.",
"Date all pastries with sticker gun by standards. (Marshmallow Dream Bars, Madelines, Chocolate Madelines, All Other Pastries)",
"Make sure to hang sticker gun back on cart & plug handheld back in."
)
var checkBoxStateArray = SparseBooleanArray()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val context = parent.context
val inflater = LayoutInflater.from(context)
val view = inflater.inflate(R.layout.checkbox_row, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int = list.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.checkbox.isChecked = checkBoxStateArray.get(position, false)
holder.checkbox.text = titles[position]
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var checkbox: CheckBox = itemView.checkbox
init {
checkbox.setOnClickListener {
if (!checkBoxStateArray.get(adapterPosition, false)) {
checkbox.isChecked = true
checkBoxStateArray.put(adapterPosition, true)
} else {
checkbox.isChecked = false
checkBoxStateArray.put(adapterPosition, false)
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire