I've check boxes displayed in the Recycler view with edit and delete options. Now I need to see / populate the data / get the Clicked / checked checkBox when I click on edit.
My Recycler View Activity
class RecView : AppCompatActivity() {
private var is_Role01: Boolean = false
private var is_Role02: Boolean = false
private var is_Role03: Boolean = false
private var is_Role04: Boolean = false
private var is_Role05: Boolean = false
private lateinit var addsBtn: FloatingActionButton
private lateinit var recv: RecyclerView
private lateinit var userList: ArrayList<UserData>
private lateinit var myAdapter: MyAdapter
private lateinit var RN: EditText
private lateinit var UN: CheckBox
private lateinit var PN: CheckBox
private lateinit var BD: CheckBox
private lateinit var AN: CheckBox
private lateinit var PR: CheckBox
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recview)
userList = ArrayList()
addsBtn = findViewById(R.id.addingBtn)
recv = findViewById(R.id.mRecycler)
myAdapter = MyAdapter(this, userList)
recv.layoutManager = LinearLayoutManager(this)
recv.adapter = myAdapter
addsBtn.setOnClickListener { addInfo() }
}
@SuppressLint("NotifyDataSetChanged")
private fun addInfo() {
val inflater = LayoutInflater.from(this)
val v = inflater.inflate(R.layout.adduser, null)
val addDialog = AlertDialog.Builder(this)
RN = v.findViewById(R.id.roleName)
UN = v.findViewById(R.id.role01)
PN = v.findViewById(R.id.role02)
BD = v.findViewById(R.id.role03)
AN = v.findViewById(R.id.role04)
PR = v.findViewById(R.id.role05)
addDialog.setView(v)
addDialog.setPositiveButton("Ok") { dialog, _ ->
val rolename = RN.text.toString()
val names = UN
val number = PN
val dob = BD
val anni = AN
val per = PR
userList.add(UserData("Role Name : $rolename", names, number, dob, anni, per))
myAdapter.notifyDataSetChanged()
Toast.makeText(this, "Adding User Information Success", Toast.LENGTH_SHORT).show()
dialog.dismiss()
UN.setOnCheckedChangeListener { compoundButton, b ->
val checked: Boolean = UN.isChecked
is_Role01 = checked
}
PN.setOnCheckedChangeListener { compoundButton, b ->
val checked: Boolean = PN.isChecked
is_Role02 = checked
}
BD.setOnCheckedChangeListener { compoundButton, b ->
val checked: Boolean = BD.isChecked
is_Role03 = checked
}
AN.setOnCheckedChangeListener { compoundButton, b ->
val checked: Boolean = AN.isChecked
is_Role04 = checked
}
PR.setOnCheckedChangeListener { compoundButton, b ->
val checked: Boolean = PR.isChecked
is_Role05 = checked
}
}
addDialog.setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show()
}
addDialog.create()
addDialog.show()
}
}
My Adapter Class Where I have edit and delete options.
class MyAdapter(val c: Context, val userList: ArrayList) : RecyclerView.Adapter<MyAdapter.UserViewHolder>() {
inner class UserViewHolder(val v: View) : RecyclerView.ViewHolder(v) {
var roleName: TextView = v.findViewById(R.id.mRoleName)
var name: CheckBox = v.findViewById(R.id.role01)
var mbNum: CheckBox = v.findViewById(R.id.role02)
var dob: CheckBox = v.findViewById(R.id.role03)
var anni: CheckBox = v.findViewById(R.id.role04)
var per: CheckBox = v.findViewById(R.id.role05)
var mMenus: ImageView
init {
mMenus = v.findViewById(R.id.mMenus)
mMenus.setOnClickListener { popupMenus(it) }
}
@SuppressLint("DiscouragedPrivateApi", "NotifyDataSetChanged")
private fun popupMenus(v: View) {
val position = userList[adapterPosition]
val popupMenus = PopupMenu(c, v)
popupMenus.inflate(R.menu.menu)
popupMenus.setOnMenuItemClickListener {
when (it.itemId) {
R.id.editText -> {
val v = LayoutInflater.from(c).inflate(R.layout.adduser, null)
val roleName = v.findViewById<EditText>(R.id.roleName)
val name = v.findViewById<CheckBox>(R.id.role01)
val number = v.findViewById<CheckBox>(R.id.role02)
val dob = v.findViewById<CheckBox>(R.id.role03)
val anni = v.findViewById<CheckBox>(R.id.role04)
val per = v.findViewById<CheckBox>(R.id.role05)
AlertDialog.Builder(c).setView(v).setPositiveButton("Ok") { dialog, _ ->
position.roleName = roleName.text.toString()
position.userName = name
position.userMb = number
position.userDob = dob
position.userAnni = anni
position.per = per
notifyDataSetChanged()
Toast.makeText(c, "User Information is Edited", Toast.LENGTH_SHORT)
.show()
dialog.dismiss()
}.setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
}.create().show()
true
}
R.id.delete -> {
/**set delete*/
AlertDialog.Builder(c).setTitle("Delete").setIcon(R.drawable.ic_warning)
.setMessage("Are you sure delete this Information")
.setPositiveButton("Yes") { dialog, _ ->
userList.removeAt(adapterPosition)
notifyDataSetChanged()
Toast.makeText(c, "Deleted this Information", Toast.LENGTH_SHORT)
.show()
dialog.dismiss()
}.setNegativeButton("No") { dialog, _ ->
dialog.dismiss()
}.create().show()
true
}
else -> true
}
}
popupMenus.show()
val popup = PopupMenu::class.java.getDeclaredField("mPopup")
popup.isAccessible = true
val menu = popup.get(popupMenus)
menu.javaClass.getDeclaredMethod("setForceShowIcon", Boolean::class.java)
.invoke(menu, true)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
val inflater = LayoutInflater.from(parent.context)
val v = inflater.inflate(R.layout.list_item, parent, false)
return UserViewHolder(v)
}
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
val newList = userList[position]
holder.roleName.text = newList.roleName
if (newList.userName.isChecked) {
holder.name.visibility = View.VISIBLE
} else {
holder.name.visibility = View.GONE
}
if (newList.userMb.isChecked) {
holder.mbNum.visibility = View.VISIBLE
} else {
holder.mbNum.visibility = View.GONE
}
if (newList.userDob.isChecked) {
holder.dob.visibility = View.VISIBLE
} else {
holder.dob.visibility = View.GONE
}
if (newList.userAnni.isChecked) {
holder.anni.visibility = View.VISIBLE
} else {
holder.anni.visibility = View.GONE
}
if (newList.per.isChecked) {
holder.per.visibility = View.VISIBLE
} else {
holder.per.visibility = View.GONE
}
}
override fun getItemCount(): Int {
return userList.size
}
}
My Data Class
data class UserData( var roleName: String, var userName: CheckBox, var userMb:CheckBox, var userDob:CheckBox, var userAnni:CheckBox, var per:CheckBox )
The Data which are given while saving initially should be populated to the edit screen. Could anyone help me out with this.
When I click on edit, the data selected in the previous page should be visible.
Aucun commentaire:
Enregistrer un commentaire