【发布时间】:2014-04-19 13:37:39
【问题描述】:
ArrayList <Car> CarList = new ArrayList<Car>();
Car carItems= new Car(carno, cartype, date, arriveTime, carcost);
CarList .add(carItems);
现在我想通过 Intent 传递 carList?
【问题讨论】:
标签: android
ArrayList <Car> CarList = new ArrayList<Car>();
Car carItems= new Car(carno, cartype, date, arriveTime, carcost);
CarList .add(carItems);
现在我想通过 Intent 传递 carList?
【问题讨论】:
标签: android
用于传递对象:
Bundle bundle = new Bundle();
ArrayList <Car> CarList = new ArrayList<Car>();
Car carItems= new Car(carno, cartype, date, arriveTime, carcost);
CarList.add(carItems);
bundle.putSerializable("carList",carList);
intent.putExtras(bundle);
用于检索:
ArrayList <Car> CarList = getIntent().getSerializableExtra("carList");
确保Car 是可序列化的:
public class Car implements Serializable {
}
【讨论】:
Car类实现Parcelable
看来您实际上必须将 Car 设为 Parcelable。
要将其添加到 Intent,请使用 putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)
编辑
要在其他活动中获取列表,请在onCreate 或onNewIntent 中执行此操作:
Intent i = getIntent();
ArrayList<Car> cars = i.getParcelableArrayListExtra("extraKeyUsedWithPutExtra");
【讨论】:
让它变得清晰,非常容易。
【讨论】: