I need to pass a string array or an arraylist to another Activity by using parcelable.
I am able to pass a string or an integer value by using parcelable.
I need some help, if possible.
Here follows my code.
MainActivity
EditText mEtSName;
EditText mEtSAge;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting a reference to EditText et_sname of the layout activity_main
mEtSName = (EditText)findViewById(R.id.et_sname);
// Getting a reference to EditText et_sage of the layout activity_main
mEtSAge = (EditText)findViewById(R.id.et_sage);
final String[] j={"Hello1","Hello2"};
// Setting onClick event listener for the "OK" button
mBtnOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Creating an instance of Student class with user input data
Student student = new Student(mEtSName.getText().toString(),
Integer.parseInt(mEtSAge.getText().toString()),
j);
Intent intent = new Intent(getBaseContext(), StudentViewActivity.class);
intent.putExtra("student",student);
startActivity(intent);
}
});
}
Here is my Parcelable Class,
String mSName;
int mSAge;
String[] a;
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mSName);
dest.writeInt(mSAge);
for( int i=0; i<a.length; i++ ){
dest.writeString(a[i]);
}
}
public Student(String sName, int sAge, String[] s){
this.mSName = sName;
this.mSAge = sAge;
this.a = s;
}
private Student(Parcel in){
this.mSName = in.readString();
this.mSAge = in.readInt();
int size = in.readInt();
a = new String[size];
for( int i=0; i<size; i++ ){
this.a[i] = in.readString();
}
}
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
@Override
public Student createFromParcel(Parcel source) {
return new Student(source);
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
}
May I know what did I do wrong?