jelic98 jelic98 - 2 months ago 8
Android Question

Pass ArrayList from to Activity

I have an

ArrayList
of
CustomInput
objects and
DataWrapper
class with getter and setter for the
ArrayList
. I want to pass
DataWrapper
from Non-Activity class to Activity class. I have tried implementing Serializable, but I get Parcelable encountered ioexception writing serializable object cause by NotSerializableException.

DataWrapper.java

public class DataWrapper implements Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<CustomInput> customInputs;

public void setFields(ArrayList<CustomInput> data) {
this.customInputs = data;
}

public ArrayList<CustomInput> getFields() {
return this.customInputs;
}
}


Non-Activiy class

public void showActivity() {
Intent intent = new Intent(request, ActivityKorak.class);
intent.putExtra("title", title);
DataWrapper dw = new DataWrapper();
dw.setFields(fields);
intent.putExtra("data", dw);

request.startActivity(intent);
}


ActivityKorak.class->onCreate()

Intent intent = getIntent();
String title = intent.getStringExtra("title");
DataWrapper dw = (DataWrapper) intent.getSerializableExtra("data");
ArrayList<CustomInput> fields = dw.getFields();

Answer

No need to create new class DataWrapper.java. Because ArrayList is serializable.

Non-Activiy class

public void showActivity() {
    Intent intent = new Intent(request, ActivityKorak.class);
    intent.putExtra("title", title);
    intent.putExtra("data", fields);

    request.startActivity(intent);
}

ActivityKorak.class->onCreate()

    Intent intent = getIntent();
    String title = intent.getStringExtra("title");
    DataWrapper dw = (DataWrapper) intent.getSerializableExtra("data");
    ArrayList<CustomInput> fields = dw.getFields();
Comments