1

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?

1 Answer 1

1

U may miss to pass the length of a, so add like this:

public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(mSName);
        dest.writeInt(mSAge);
        dest.writeInt(a.length);
        for( int i=0; i<a.length; i++ ){
            dest.writeString(a[i]);
        }
    }

Or use writeArray & readArray like this:

public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(mSName);
    dest.writeInt(mSAge);
    dest.writeArray(a);
}

private Student(Parcel in){
    this.mSName = in.readString();
    this.mSAge = in.readInt();

    Object[] objects = in.readArray(null);
    a = new String[objects.length];
    for( int i = 0; i < size; i++){
      this.a[i] = objects[i];
    }
   }

For list, try to use writeList & readList like this:

public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(mSName);
    dest.writeInt(mSAge);
    dest.writeList(mLists);
}

private Student(Parcel in){
    this.mSName = in.readString();
    this.mSAge = in.readInt();

    this.mLists = new ArrayList<String>();
    in.readList(mLists, null);
   }

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.