如何将多个选定的UTEM从recyclerview发送到另一个活动-Kotlin Android

发布于 2025-01-22 18:01:47 字数 1522 浏览 0 评论 0原文

我有选择多个项目的联系人列表,然后在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绻影浮沉 2025-01-29 18:01:47

当您在selectItem方法中选择联系人时,应该创建mutablelist在此列表内部的MutableList之类的联系人您之前将存储所选的联系人,因为您可以删除/更新所选的联系人列表您应该使用mutablelist选择所有联系人后,您应该有一个按钮可以转到下一个屏幕。在那里您只能发送contactmodel的mutablelist,

让我为您做一些伪代码,

private fun selectItem(holder: ContactAdapter.ContactViewHolder, item: ContactModel, position: Int) {
        isEnable = true
        itemSelected.add(position)
        item.isSelect = true
        holder.linlay.setBackgroundColor(Color.YELLOW)
        if(item.isSelect)
        myMutableContactList.add(item)
        else
        myMutableContactList.remove(item)
    }

记住首先创建Mutablelist,例如

val mymutablecontactlist = mutableBlelistof&lt; contactmodel&gt;()

请 发送此列表

下一个屏幕只需在按下按钮时 ,然后转到下一个片段/活动,一个好练习将是将此mutablelist转换为list,因为它不会更改Beign发送后。

您可以通过简单使用tolist()在mutablelist上做到这一点

When you select a contact in your selectItem method you should create a MutableList 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 using MutableList 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 ContactModel

Let me do some pseudocode for you

private fun selectItem(holder: ContactAdapter.ContactViewHolder, item: ContactModel, position: Int) {
        isEnable = true
        itemSelected.add(position)
        item.isSelect = true
        holder.linlay.setBackgroundColor(Color.YELLOW)
        if(item.isSelect)
        myMutableContactList.add(item)
        else
        myMutableContactList.remove(item)
    }

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 a list since it will not change after beign send.

You can do it by simple using toList() on your mutableList

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文