我想在两个Activity之间传递ArrayList对象list,该对象里有多个students对象。在网上搜有两种实现方法:一种是实现Serializable接口,一种是实现Parcelable接口。但传递多个对象只能用实现Parcelable接口这种方法。实现该接口的类我已经写好了,现在问题是数据是如何放入的,又是如何取出来的,老是出错。(声明:list不能直接放到bundle里面的)。我有搜到这种方法,但不明白接收端里的msg是怎么来的,按理说消息是不能再Activity之间传递的?求解答总结:传递数据部分、接收数据部分应该怎样来写??
Intent it = new Intent(OneActivity.this, TwoMaxActivity.class);
it.putExtra("f", (Serializable) list);
OneActivity.this.startActivity(it); 接受:
Intent it = getIntent();
list = (List<Furniture>) it.getSerializableExtra("f");
2、两个activity之间传值,可以用intent啊,用键值对,方便、简单。。
你贴贴出的这个例子是使用Handler发送消息来实现的,msg从而而来想必知道了下面有个简单的实现发送:
/** 两个Activity之间进行List Object传输 */
Intent myIntent = new Intent();
ArrayList<Person> personList = new ArrayList<Person>();
Parcel parcel = Parcel.obtain();
Person person = Person.CREATOR.createFromParcel(parcel);
person.setId(10);
person.setName("bingbing");
personList.add(person);
parcel.recycle();
myIntent.putParcelableArrayListExtra("list", personList);
myIntent.setClass(this, SecondActivity.class);
startActivity(myIntent);接收:
ArrayList<Person> list = getIntent().getParcelableArrayListExtra("list");
int fistOneId = list.get(0).getId();
Log.i(TAG, "id : " + fistOneId);