点击mainActivity中的按钮,把Arraylist<Person> mList传递给MainActivity2并在TextView显示出来。直接看代码:
public class Person implements Parcelable{//实现 Parcelable String name; int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } //下面三个方法才是关键 @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点击事件 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; } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。