gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:13:21 25 4
gpt4 key购买 nike

我需要使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com