[英]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:编辑您的问题后编辑:
Serializable
, then you will just putExtra
and getSerializableExtra
Serializable
,那么您只需putExtra
和getSerializableExtra
Parcelable
, then you will just putExtra
and getParcelableExtra
Parcelable
,那么您只需putExtra
和getParcelableExtra
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
Serializable
和Parcelable
的区别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.