我有2个活动,我想传递一个对象的数组列表并将其显示在ListView上
活动A:
btnGuardar.setOnClickListener{
if(edtNombre.text.toString().equals("") ||
edtApellido.text.toString().equals("") ||
edtFecha.text.toString().equals("")){
Toast.makeText(this@Main2Activity, "Debes llenar todos los campos!", Toast.LENGTH_SHORT).show()
}else{
var estadoSel : String
estadoSel = estado.onItemSelectedListener.toString()
var per = Persona(edtNombre.text.toString(),edtApellido.text.toString(),estadoSel,edtFecha.text.toString())
personas.add(per)
val intent = Intent(this@Main2Activity,Main3Activity::class.java)
//intent.putExtra("Personas", personas as Serializable)
intent.putParcelableArrayListExtra("Personas", ArrayList(personas))
edtFecha.text = null
edtApellido.text = null
edtNombre.text = null
estado.setSelection(0)
Toast.makeText(this@Main2Activity, "Registro Guardado", Toast.LENGTH_SHORT).show()
}
}
活动B:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main3)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
//val intent = Intent()
val bundle = getIntent().extras
var arr = bundle?.getParcelableArrayList<Persona>("Personas")!!
//var arr : ArrayList<Persona> = (ArrayList<Persona>())intent.getSerializableExtra("Personas")
//intent.getSerializableExtra("Personas")
//arr = intent.extras!!.get("Personas")
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, arr)
listaPersonas.adapter = adapter
}
但是我遇到了空指针异常
答案 0 :(得分:3)
在您的应用的android作用域下的build.gradle中添加此内容并同步gradle-
androidExtensions {
experimental = true
}
然后按照以下步骤使您的对象类可重复使用-
@Parcelize
data class Persona() : Parcelable {
}
现在,您可以按意图发送和接收Parcelable列表。
注意*-打包是实验性功能。
答案 1 :(得分:2)
要通过Model类上的意图实现Parcelable
接口传递数据列表。
人员模型类别:
import android.os.Parcel
import android.os.Parcelable
data class Person(val name: String?, val age: Int) : Parcelable {
constructor(source: Parcel) : this(
source.readString(),
source.readInt()
)
override fun describeContents() = 0
override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
writeString(name)
writeInt(age)
}
companion object {
@JvmField
val CREATOR: Parcelable.Creator<Person> = object : Parcelable.Creator<Person> {
override fun createFromParcel(source: Parcel): Person = Person(source)
override fun newArray(size: Int): Array<Person?> = arrayOfNulls(size)
}
}
}
MainActivity:
class MainActivity : AppCompatActivity() {
var personList = arrayListOf<Person>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
personList.add(Person("Person1", 31))
personList.add(Person("Person2", 32))
personList.add(Person("Person3", 33))
var intent = Intent(this, BActivity::class.java)
intent.putParcelableArrayListExtra("personList", personList)
Handler().postDelayed( {startActivity(intent)}, 2000)
}
}
B活动:
class BActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_b)
val extras = intent.extras
val personList = extras?.getParcelableArrayList<Person>("personList")
if (personList != null) {
personList.forEach { Log.i("BActivity", "Name: ${it.name} Age: ${it.age}" ) }
}else{
Log.i("BActivity", "PersonList in null");
}
}
}
答案 2 :(得分:1)
第1步。在Android作用域内的应用程序build.gradle中添加此内容。
androidExtensions {
experimental = true
}
第2步:使对象类实现可打包
@Parcelize
class Persona():Parcelable {
}
步骤3。在活动A中,通过意图发送数据:
val intent = Intent(this, B::class.java)
intent.putParcelableArrayListExtra("list", personaArrayList)
startActivity(intent)
步骤4:在活动B中接收数据:
val list = intent.getParcelableArrayListExtra<Persona>("list")
答案 3 :(得分:0)
在活动B中获取列表,如下所示
var arr = this.getIntent().getParcelableArrayListExtra<Parcelable>("Personas")
并确保您的Persona
是extend Parcelable
,如下所示。
class Persona() : Parcelable {
// ....
@Override
public void writeToParcel(Parcel dest, int flags) {
//....
}
private void readFromParcel(Parcel in) {
//....
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Creator<Persona> {
override fun createFromParcel(parcel: Parcel): Persona{
return Persona(parcel)
}
override fun newArray(size: Int): Array<Persona?> {
return arrayOfNulls(size)
}
}
}
答案 4 :(得分:0)
您的“活动A”中的发送数组列表带有
intent.putParcelableArrayListExtra("Personas", personas)
在您的活动B中,您将获得数组列表
intent.getParcelableArrayListExtra("Personas")
在android代码中的应用的build.gradle中添加此代码并与gradle同步-
androidExtensions {
experimental = true
}
数据类
@Parcelize data class Persona(
val name: String,
) : Parcelable