android 在activity之间如何传递复杂数据类型
扫描二维码
随时随地手机看文章
在Activity传送复杂数据时,(这里用ArrayList
首先建立两个javabean,一个实现Serializable接口,一个实现Parcelable接口:
package org.example.passdata; import java.io.Serializable; public class Student implements Serializable { private static final long serialVersionUID = 6130446011020753806L; private String id; private String name; public Student(String id, String name) { this.id = id; this.name = name; } public String getId() { return this.id; } public void setId(String id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "id:" + this.id + " name:" + this.name; } }
package org.example.passdata; import android.os.Parcel; import android.os.Parcelable; public class Teacher implements Parcelable { private String id; private String name; public Teacher(String id, String name) { this.id = id; this.name = name; } public String getId() { return this.id; } public void setId(String id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(id); dest.writeString(name); } @Override public String toString() { return "id:" + this.id + " name:" + this.name; } public static final Parcelable.CreatorCREATOR = new Parcelable.Creator() { @Override public Teacher createFromParcel(Parcel source) { Teacher teacher = new Teacher(source.readString(), source.readString()); return teacher; } @Override public Teacher[] newArray(int size) { return null; } }; }
然后在MainActrivity中发送数据:
package org.example.passdata; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class PassDataActivity extends Activity implements OnClickListener { private Button normalBtn, serializableBtn, parcelBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); normalBtn = (Button) findViewById(R.id.normalbtn); serializableBtn = (Button) findViewById(R.id.serializablebtn); parcelBtn = (Button) findViewById(R.id.parcelbtn); normalBtn.setOnClickListener(this); serializableBtn.setOnClickListener(this); parcelBtn.setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId() == normalBtn.getId()) { // 普通传送 } else if (v.getId() == serializableBtn.getId()) { // Serializable序列化传送 ArrayListlist = new ArrayList(); Student student = new Student("001", "naki"); list.add(student); Intent intent = new Intent(this, SecondActivity.class); intent.putExtra("STUDENT_INFO", list); startActivity(intent); } else if (v.getId() == parcelBtn.getId()) { // parcel序列化传送 ArrayListlist = new ArrayList(); Teacher teacher1 = new Teacher("001", "techaer1"); list.add(teacher1); Intent intent = new Intent(this, SecondActivity.class); intent.putExtra("TEACHER_INFO", list); startActivity(intent); } } }
在接收的Activity中接收数据:
package org.example.passdata; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); // serializable // ArrayListlist = (ArrayList) intent.getSerializableExtra("STUDENT_INFO"); // TextView textView = new TextView(this); // textView.setText(list.get(0).toString()); // setContentView(textView); // parcelble ArrayListlist = intent.getParcelableArrayListExtra("TEACHER_INFO"); TextView textView = new TextView(this); textView.setText(list.get(0).toString()); setContentView(textView); } }