如何在android

时间:2015-04-22 10:27:46

标签: android parcelable

我需要使用parcelable将字符串数组或arraylist传递给另一个Activity 我可以使用parcelable传递字符串或整数值。

如果可能,我需要一些帮助。

以下是我的代码。

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);
            }
        });
    }

这是我的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];
        }
    };
}

我可以知道我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

你可能会错过通过length的{​​{1}},所以请添加如下:

a

或使用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]); } } &amp; writeArray喜欢这样:

readArray

对于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]; } } ,请尝试使用list&amp; writeList喜欢这样:

readList

希望它有所帮助。