android - 如何在android中使用parcelable传递字符串数组

标签 android parcelable

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

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

下面是我的代码。

主要 Activity

    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 类,

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

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

最佳答案

U可能会漏传alength,所以这样添加:

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

或者像这样使用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,尝试像这样使用 writeListreadList:

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

希望对您有所帮助。

关于android - 如何在android中使用parcelable传递字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29794685/

相关文章:

android - v.getTag() 返回 null 而不是 ViewHolder

java - Android float 操作按钮未被识别

android - 我想将 XML 数据导入 sqlite 数据库并将其保存在表中。保存后我想在我的应用程序中使用它

android - 缺少服务时,isGooglePlayServicesAvailable() 返回 ConnectionResult.SUCCESS

Android Parcelable readStringArray 错误的数组长度

Android:包裹类包装错误

Android:将 Parcelable 转换为 JSON

android - Parcelable 中的 List<String> readStringArray

java - 如何使用 NDK 将 Lame 3.99.5 添加到 Android Studio?

java - 在 Intent 之间传递 Parcelable 对象