Intent傳遞複雜ArrayList物件(Parcelable)
阿新 • • 發佈:2018-11-23
- 自定義實體實現Parcelable介面
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by HTT on 2017/10/25.
*/
public class Terminal implements Parcelable {
private String id;
private String name;
private String address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPname() {
return pname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//此方法一定要實現
public static final Creator<Terminal> CREATOR = new Creator<Terminal>() {
@Override
public Terminal createFromParcel(Parcel parcel) {
Terminal ter = new Terminal();
ter.id = parcel.readString();
ter.name = parcel.readString();
ter.address = parcel.readString();
return ter;
}
@Override
public Terminal[] newArray(int i) {
return new Terminal[i];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(id);
parcel.writeString(name);
parcel.writeString(address);
}
}
- 跳轉頁面設定
ArrayList<Terminal> selectTers = new ArrayList<>();
Intent intent = Intent(MainActivity.this,SelectTerActivity.class);
intent.putParcelableArrayListExtra("selectTers", selectTers);
startActivity(intent);
接收頁面
Intent intent = this.getIntent();
ArrayList<Terminal> selectTers = intent.getParcelableArrayListExtra("selectTers");
這樣就可以傳遞複雜的ArrayList物件了。
參考:
Intent之物件傳遞(Parcelable傳遞物件和物件集合)
Intent之物件傳遞(Serializable傳遞物件和物件集合)
Intent傳遞Parcelable List物件