简体   繁体   English

Kotlin 无法从 Model Class 与 Putextra 发送数据

[英]Kotlin Can't Send Data From Model Class With Putextra

I have created a structure like the one below.我创建了一个类似下面的结构。 When I click the list in the first activity, I want the information about that item in the second activity.当我单击第一个活动中的列表时,我想要第二个活动中有关该项目的信息。 However, I cannot send data with putextra.但是,我无法使用 putextra 发送数据。 I will be very happy if you are interested.I do not know the subject fully.如果你有兴趣,我会很高兴。我不完全了解这个主题。 Therefore, if anyone is interested, I will assign the necessary codes.因此,如果有人有兴趣,我会分配必要的代码。

Customers.kt客户.kt

    val listView=findViewById<ListView>(R.id.listView)
    val list=ArrayList<CustomerModel>()

    listView.setOnItemClickListener { adapterView, view, i, l ->

        val intent=Intent(applicationContext,CustomersDetails::class.java)
      //I think that's not true   intent.putExtra("Name",list[i])
        startActivity(intent)
    }

CustomersDetails.kt客户详情.kt

 val intent=intent
    choosenCustomer= intent.getStringExtra("Name").toString()

CustomerModel.kt客户模型.kt

class CustomerModel(val title:String, val description:String,val img:Int): Parcelable {
constructor(parcel: Parcel) : this(
    parcel.readString()!!,
    parcel.readString()!!,
    parcel.readInt()
) {
}

override fun writeToParcel(parcel: Parcel, flags: Int) {
    parcel.writeString(title)
    parcel.writeString(description)
    parcel.writeInt(img)
}

override fun describeContents(): Int {
    return 0
}

companion object CREATOR : Parcelable.Creator<CustomerModel> {
    override fun createFromParcel(parcel: Parcel): CustomerModel {
        return CustomerModel(parcel)
    }

    override fun newArray(size: Int): Array<CustomerModel?> {
        return arrayOfNulls(size)
    }
}

} }

CustomerAdapter.kt客户适配器.kt

class CustomerAdapter(var mCtx:Context,val resources: Int,var items:List<CustomerModel>):
ArrayAdapter<CustomerModel>(mCtx,resources,items) {

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    val layoutInflater: LayoutInflater= LayoutInflater.from(mCtx)
    val view:View=layoutInflater.inflate(resources,null)

    val imageView: ImageView=view.findViewById(R.id.image)
    val titleTextView: TextView=view.findViewById(R.id.cuText)
    val descriptionTextView:TextView=view.findViewById(R.id.ofText)

    var mItem:CustomerModel=items[position]

    imageView.setImageDrawable(mCtx.resources.getDrawable(mItem.img))
    titleTextView.text=mItem.title
    descriptionTextView.text=mItem.description
    return view
}

} }

You shouldn't redifine the intent as you did, Instead of你不应该像以前那样重新定义意图,而不是

val intent=intent
    choosenCustomer= intent.getStringExtra("Name").toString()

You should use:你应该使用:

choosenCustomer= intent.getStringExtra("Name").toString()

The first snippet the intent value becomes null I think我认为意图值的第一个片段变为 null

One option could be letting your custom class implement the Serializable interface and then you can pass object instances in the intent extra using the putExtra(Serializable..) variant of the Intent#putExtra() method.一种选择是让您的自定义 class 实现 Serializable 接口,然后您可以使用 Intent#putExtra() 方法的 putExtra(Serializable..) 变体在 Intent extra 中传递 object 实例。

You can extract Parcelable obj using intent.getParcelableExtra(/ key /"Name")您可以使用 intent.getParcelableExtra(/ key /"Name") 提取 Parcelable obj

CustomerModel needs to be serializable. CustomerModel 需要可序列化。

class CustomerModel : Serializable

See more: https://spin.atomicobject.com/2016/11/28/sending-data-actions-android/查看更多: https://spin.atomicobject.com/2016/11/28/sending-data-actions-android/

Edited after you edit your question:编辑您的问题后编辑:

  • If you use Serializable , then you will just putExtra and getSerializableExtra如果您使用Serializable ,那么您只需putExtragetSerializableExtra
  • If you use Parcelable , then you will just putExtra and getParcelableExtra如果您使用Parcelable ,那么您只需putExtragetParcelableExtra

Why do we serialize objects?为什么要序列化对象?

Serialization of the object is usually needed when you send your object over a network or store it in files because the network infrastructure and hardware components used for storage understand bits and bytes, but not objects.当您通过网络发送 object 或将其存储在文件中时,通常需要 object 的序列化,因为用于存储的网络基础设施和硬件组件了解位和字节,但不了解对象。 In Android, when you want to send an object to another activity from any activity, you cannot send it as you do in primitive types.在 Android 中,当您想将 object 从任何活动发送到另一个活动时,您不能像在原始类型中那样发送它。

Differences between Serializable and Parcelable SerializableParcelable的区别

  • Serializable interface is very easy to implement.可序列化的接口很容易实现。 It is a marker interface and by implementing this, your POJO class is able to be serialized and deserialized;它是一个标记接口,通过实现它,您的 POJO class 能够被序列化和反序列化; whereas Parcelable is not a marker interface, hence it has some methods that you will have to override when you implement this in your class.而 Parcelable 不是标记接口,因此它有一些方法,当您在 class 中实现它时,您必须重写这些方法。
  • Serializable interface is not a part of Android SDK and it uses reflection for marshaling operations and creates lots of temp objects.可序列化接口不是 Android SDK 的一部分,它使用反射进行编组操作并创建大量临时对象。 In Parcelable, you are able to choose which field you want to serialize.在 Parcelable 中,您可以选择要序列化的字段。
  • Because of the temp object creation and garbage collection, Serialization is slower than Parcelable.由于临时 object 创建和垃圾收集,序列化比 Parcelable 慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM