Issue
I have 2 Activities, I want to pass an arraylist of an Object and show it on a ListView
Activity 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()
}
}
Activity 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
}
But I'm getting a Null Pointer Exception
Solution
In your activity B to get your list try like the following
var arr = this.getIntent().getParcelableArrayListExtra<Parcelable>("Personas")
and make sure your Persona
is extend Parcelable
like below.
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)
}
}
}
Answered By - Jakir Hossain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.