您好,登錄后才能下訂單哦!
點(diǎn)擊mainActivity中的按鈕,把Arraylist<Person> mList傳遞給MainActivity2并在TextView顯示出來(lái)。直接看代碼:
public class Person implements Parcelable{//實(shí)現(xiàn) Parcelable String name; int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } //下面三個(gè)方法才是關(guān)鍵 @Override public int describeContents() { // TODO Auto-generated method stub return 0; } public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() { public Person createFromParcel(Parcel source) { Person person = new Person("ccc", 12); person.name = source.readString(); person.age = source.readInt(); return person; } public Person[] newArray(int size) { return new Person[size]; } }; @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(age); } ////////////////////////////////////////////////// @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class MainActivity extends Activity { private ArrayList<Person> mList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mList = new ArrayList<Person>(); Person person1 = new Person("aaa", 12); Person person2 = new Person("bbb", 21); mList.add(person1); mList.add(person2); } public void btnClick(View v) {//button點(diǎn)擊事件 Intent intent = new Intent(this, MainActivity2.class); Bundle bundle = new Bundle(); bundle.putParcelableArrayList("mList", mList); // intent.putParcelableArrayListExtra("mList", mList); intent.putExtras(bundle); startActivity(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
public class MainActivity2 extends Activity { private TextView mText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activity2); mText = (TextView) findViewById(R.id.tv2); ArrayList<Person> serializable = (ArrayList<Person>) getIntent().getExtras().getSerializable("mList"); mText.setText(serializable.toString()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main_activity2, menu); return true; } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。