如何将Arraylist从回收库将其传递到其他活动-Kotlin?

发布于 2025-01-23 03:40:04 字数 3682 浏览 0 评论 0原文

我正在尝试传递ArrayList的多个值,这些值正在回收到另一个活动。但是,每当我获得零值或任何内存位置而不是值时。 这是我的适配器代码


class ContactAdapter(private var contact:List<ContactModel>, private var context: Context, private val showMenu: (Boolean) -> Unit): RecyclerView.Adapter<ContactAdapter.ContactViewHolder>() {
    private var isEnable =false
    private val itemSelected = mutableListOf<Int>()
    val myMutableContactList = ArrayList<ContactModel>()
    class ContactViewHolder(private val view: View):RecyclerView.ViewHolder(view) {
        val linlay:LinearLayout = view.findViewById(R.id.linlay)
        val name: TextView = view.findViewById(R.id.name)
        val number: TextView = view.findViewById(R.id.number)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContactViewHolder {
        val layout_ =LayoutInflater.from(parent.context).inflate(R.layout.contact_list,parent,false)
        return ContactViewHolder(layout_)
    }

    override fun onBindViewHolder(holder: ContactViewHolder, position: Int) {
        val item = contact[position]
        holder.name.text = item.name.toString()
        holder.number.text = item.number.toString()
        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
        showMenu(true)
        holder.linlay.setBackgroundColor(Color.YELLOW)
        if(item.isSelect){
            myMutableContactList.add(item)}
        else{
            myMutableContactList.remove(item)}}

    override fun getItemCount(): Int {
        return contact.size
    }
    fun sendSelectedItem(){
        if(itemSelected.isNotEmpty()){
            val intent = Intent(context,HomeActivity::class.java)
            intent.putParcelableArrayListExtra("key",myMutableContactList)
            context.startActivity(intent)
            isEnable = false
            itemSelected.clear()
        } }}

值选择多个项目后传递,并且下一个活动的按钮在菜单栏中。 我正在使用Getter Setter模型类,而不是数据类 这是我

class ContactModel() :Parcelable {
    var name: String? = null
    var number: String? = null
    var isSelect: Boolean = false
    constructor(parcel: Parcel) : this() {
        name = parcel.readString()
        number = parcel.readString()
        isSelect = parcel.readByte() != 0.toByte()
    }
    fun setNames(name: String) {
        this.name = name
    }
    fun getNumbers(): String {
        return number.toString()
    }
    fun setNumbers(number: String) {
        this.number = number
    }
    fun getNames(): String {
        return name.toString()
    }

其他活动的模型类代码

  private var list_: ArrayList<ContactModel>? = null
  list_ = intent.getParcelableArrayListExtra("key") 
         val contactModel = ContactModel()
        val name = contactModel.getName()
        val number =  contactModel.getNumbers()
        list_!!.add(contactModel)
        Log.d("name>>", name + "  " + number)
        Toast.makeText(this, name.toString(), Toast.LENGTH_SHORT).show()

I'm trying to pass multiple value of arraylist, which are in recyclerview to Another Activity. But every time I get a null value or either memory location instead of values.
Here is my adapter code


class ContactAdapter(private var contact:List<ContactModel>, private var context: Context, private val showMenu: (Boolean) -> Unit): RecyclerView.Adapter<ContactAdapter.ContactViewHolder>() {
    private var isEnable =false
    private val itemSelected = mutableListOf<Int>()
    val myMutableContactList = ArrayList<ContactModel>()
    class ContactViewHolder(private val view: View):RecyclerView.ViewHolder(view) {
        val linlay:LinearLayout = view.findViewById(R.id.linlay)
        val name: TextView = view.findViewById(R.id.name)
        val number: TextView = view.findViewById(R.id.number)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContactViewHolder {
        val layout_ =LayoutInflater.from(parent.context).inflate(R.layout.contact_list,parent,false)
        return ContactViewHolder(layout_)
    }

    override fun onBindViewHolder(holder: ContactViewHolder, position: Int) {
        val item = contact[position]
        holder.name.text = item.name.toString()
        holder.number.text = item.number.toString()
        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
        showMenu(true)
        holder.linlay.setBackgroundColor(Color.YELLOW)
        if(item.isSelect){
            myMutableContactList.add(item)}
        else{
            myMutableContactList.remove(item)}}

    override fun getItemCount(): Int {
        return contact.size
    }
    fun sendSelectedItem(){
        if(itemSelected.isNotEmpty()){
            val intent = Intent(context,HomeActivity::class.java)
            intent.putParcelableArrayListExtra("key",myMutableContactList)
            context.startActivity(intent)
            isEnable = false
            itemSelected.clear()
        } }}

Value is passed after selecting multiple items and the button for next activity is in the menu bar.
I'm using getter setter model class instead of the data class
here is my model class

class ContactModel() :Parcelable {
    var name: String? = null
    var number: String? = null
    var isSelect: Boolean = false
    constructor(parcel: Parcel) : this() {
        name = parcel.readString()
        number = parcel.readString()
        isSelect = parcel.readByte() != 0.toByte()
    }
    fun setNames(name: String) {
        this.name = name
    }
    fun getNumbers(): String {
        return number.toString()
    }
    fun setNumbers(number: String) {
        this.number = number
    }
    fun getNames(): String {
        return name.toString()
    }

code of other activity

  private var list_: ArrayList<ContactModel>? = null
  list_ = intent.getParcelableArrayListExtra("key") 
         val contactModel = ContactModel()
        val name = contactModel.getName()
        val number =  contactModel.getNumbers()
        list_!!.add(contactModel)
        Log.d("name>>", name + "  " + number)
        Toast.makeText(this, name.toString(), Toast.LENGTH_SHORT).show()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

倾其所爱 2025-01-30 03:40:04

我有一个主意,但不确定是否是最佳实践。您可以在要发送数据的活动中创建一个伴随对象,并在伴随对象中创建一个类型lateinit var的变量。即使适配器不活动活动,您也可以将其初始化。

I have an idea but not sure is best practice or not. You can create a companion object in the activity where you want to send the data and in the companion object create a variable of type lateinit var. You can initialize it even when activity is not active from the adapter.

温柔嚣张 2025-01-30 03:40:04

如果您阅读了此功能的文档,它说:

名称必须包括一个包前缀,例如app com.android.contacts将使用“ com.android.contacts.showall”之类的名称。

您没有这样做,因为您只是使用“键”。

您不应该在课堂上有一堆“获取”和“设置”功能,这些功能可以冗余地管理您的属性。 Kotlin中未使用“ Getter”和“ Setter”功能,因为属性填补了该角色。

您的包裹类别缺少创建者功能和WritEtoParcel函数,因此无法正常工作。您应该考虑使用 parcelize插件,因此您不必自己添加这些插件。

If you read the documentation of this function, it says:

The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

which you did not do, since you're simply using "key".

You should not have a bunch of "get" and "set" functions in your class that redundantly manage your properties. "getter" and "setter" functions are not used in Kotlin because properties fill that role.

Your Parcelable class is lacking a CREATOR function and a writeToParcel function, so it cannot work. You should consider using the Parcelize plugin so you don't have to add those yourself.

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