如何将多个选定的UTEM从recyclerview发送到另一个活动-Kotlin Android
我有选择多个项目的联系人列表,然后在Recyclerview中选择多个项目后,我将其传递给另一个活动。 这是我的适配器类
override fun onBindViewHolder(holder: ContactViewHolder, position: Int) {
val item = contact[position]
holder.name.text = item.name
holder.number.text = item.number
holder.linlay.setOnLongClickListener{
selectItem(holder,item,position)
true
}
holder.linlay.setOnClickListener{
if(itemSelected.contains(position)){
itemSelected.remove(position)
holder.linlay.setBackgroundColor(Color.DKGRAY)
item.isSelect = false
if(itemSelected.isEmpty()){
isEnable=false
}
}
else if(isEnable){
selectItem(holder,item,position)
}
}
}
private fun selectItem(holder: ContactAdapter.ContactViewHolder, item: ContactModel, position: Int) {
isEnable = true
itemSelected.add(position)
item.isSelect = true
holder.linlay.setBackgroundColor(Color.YELLOW)
}
,其中要参加另一个活动的按钮是在MainAttivity类中而不是回收库
class Mainactivity : AppComapctActivity(){
ovveride fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn_send = findViewById<Button>(R.id.btn_send)
btn_send.setOnClickListner{
val intent = Intent(this,HomeActivity::class.java)
startActivity(intent)
}
}
I have list of contacts in where i select multiple items and pass to another activity ,In Recyclerview after selecting multiple items how do i pass it to another activity
here is my Adapter class
override fun onBindViewHolder(holder: ContactViewHolder, position: Int) {
val item = contact[position]
holder.name.text = item.name
holder.number.text = item.number
holder.linlay.setOnLongClickListener{
selectItem(holder,item,position)
true
}
holder.linlay.setOnClickListener{
if(itemSelected.contains(position)){
itemSelected.remove(position)
holder.linlay.setBackgroundColor(Color.DKGRAY)
item.isSelect = false
if(itemSelected.isEmpty()){
isEnable=false
}
}
else if(isEnable){
selectItem(holder,item,position)
}
}
}
private fun selectItem(holder: ContactAdapter.ContactViewHolder, item: ContactModel, position: Int) {
isEnable = true
itemSelected.add(position)
item.isSelect = true
holder.linlay.setBackgroundColor(Color.YELLOW)
}
Where button for going to another activity is instead of recyclerview is in mainActivity class
class Mainactivity : AppComapctActivity(){
ovveride fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn_send = findViewById<Button>(R.id.btn_send)
btn_send.setOnClickListner{
val intent = Intent(this,HomeActivity::class.java)
startActivity(intent)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在
selectItem
方法中选择联系人时,应该创建mutablelist
在此列表内部的MutableList之类的联系人您之前将存储所选的联系人,因为您可以删除/更新所选的联系人列表您应该使用mutablelist
选择所有联系人后,您应该有一个按钮可以转到下一个屏幕。在那里您只能发送contactmodel的mutablelist,让我为您做一些伪代码,
记住首先创建Mutablelist,例如
val mymutablecontactlist = mutableBlelistof&lt; contactmodel&gt;()
请 发送此列表
下一个屏幕只需在按下按钮时 ,然后转到下一个片段/活动,一个好练习将是将此
mutablelist
转换为list
,因为它不会更改Beign发送后。您可以通过简单使用
tolist()
在mutablelist上做到这一点When you select a contact in your
selectItem
method you should create aMutableList
of contacts like MutableList inside of this list you will store the selected contacts previously, since you can remove/update the selected contact list you should be usingMutableList
after you select all your contacts you should have a button to go to the next screen. There you should only send the MutableList of ContactModelLet me do some pseudocode for you
remember to create first the mutableList like
val myMutableContactList = mutableListOf<ContactModel>()
and then if you have a button to go to the next screen just send this list
When you press the button and go to the next fragment/activity a good practice will be to convert this
mutablelist
to alist
since it will not change after beign send.You can do it by simple using
toList()
on your mutableList