Acitivity之间的传递数据有三种
一.基本数据类型的传递
二.传递数据包Bundle(集装箱)
三.传递对象集合(重点)
现在我们就来讲这个重点
效果图:
首先我们需要两个XML:
第一个:<span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cookie.android0616activityjump.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_main_uname"
android:hint="请输入用户名"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="写心情"
android:onClick="writeThink"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_main_showThink"/>
</LinearLayout></span>
<span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cookie.android0616activityjump.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_main_uname"
android:hint="请输入用户名"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="写心情"
android:onClick="writeThink"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_main_showThink"/>
</LinearLayout></span>
第二个:
<span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cookie.android0616activityjump.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_think_showName"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="5"
android:hint="请输入心情"
android:gravity="top"
android:id="@+id/et_think_someThink"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="确定"
android:onClick="ok"/>
</LinearLayout></span>
<span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cookie.android0616activityjump.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_think_showName"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="5"
android:hint="请输入心情"
android:gravity="top"
android:id="@+id/et_think_someThink"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="确定"
android:onClick="ok"/>
</LinearLayout></span>
再去创建一个entity实体类
<span style="color:#000000;">//java序列化:Serializable 只可以传对象
//安卓序列化:Parcelable 既可以传对象,也可以传集合
public class Person implements Parcelable{
private int pid;
private String pname;
private int page;
public Person() {
}
public Person(int pid, String pname, int page) {
this.pid = pid;
this.pname = pname;
this.page = page;
}
//从序列化中读取方法
protected Person(Parcel in) {
pid = in.readInt();
pname = in.readString();
page = in.readInt();
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
@Override
public String toString() {
return "Person{" +
"pid=" + pid +
", pname='" + pname + '\'' +
", page=" + page +
'}';
}
@Override
public int describeContents() {
return 0;
}
//Parcel:序列化
//把属性写到序列化中
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(pid);
dest.writeString(pname);
dest.writeInt(page);
}
}</span>
<span style="color:#000000;">//java序列化:Serializable 只可以传对象
//安卓序列化:Parcelable 既可以传对象,也可以传集合
public class Person implements Parcelable{
private int pid;
private String pname;
private int page;
public Person() {
}
public Person(int pid, String pname, int page) {
this.pid = pid;
this.pname = pname;
this.page = page;
}
//从序列化中读取方法
protected Person(Parcel in) {
pid = in.readInt();
pname = in.readString();
page = in.readInt();
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
@Override
public String toString() {
return "Person{" +
"pid=" + pid +
", pname='" + pname + '\'' +
", page=" + page +
'}';
}
@Override
public int describeContents() {
return 0;
}
//Parcel:序列化
//把属性写到序列化中
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(pid);
dest.writeString(pname);
dest.writeInt(page);
}
}</span>
接下来就是java代码:
第一个:<span style="color:#000000;">public class MainActivity extends AppCompatActivity {
private EditText et_main_text;
private TextView tv_main_showThink;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_text = (EditText) findViewById(R.id.et_main_uname);
tv_main_showThink = (TextView) findViewById(R.id.tv_main_showThink);
// String thinks=getIntent().getStringExtra("thinks");
// tv_main_showThink.setText(thinks);
}
//获取其他人设值过来的结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String thinks=data.getStringExtra("thinks");
tv_main_showThink.setText(thinks);
}
public void writeThink(View view){
String uname=et_main_text.getText().toString();
Intent intent=new Intent(this,ThinkActivity.class);
//传递基本数据类型
// intent.putExtra("uname",uname);
// intent.putExtra("uage",19);
//传递Bundle
// Bundle bundle=new Bundle();
// bundle.putString("uname",uname);
// bundle.putInt("uage",10);
// intent.putExtra("bundle",bundle);
//传递对象
// Person person=new Person(1,"明明",10);
//传递对象集合
Person person1=new Person(1,"求其",20);
Person person2=new Person(2,"微微",12);
Person person3=new Person(3,"琪琪",30);
ArrayList<Person> persons=new ArrayList<>();
persons.add(person1);
persons.add(person2);
persons.add(person3);
//传递对象
//intent.putExtra("person",person);
//传递对象集合
intent.putParcelableArrayListExtra("persons",persons);
// startActivity(intent);
//带有目的的跳转(请求码,结果码)
startActivityForResult(intent,0x23);
}
}</span>
<span style="color:#000000;">public class MainActivity extends AppCompatActivity {
private EditText et_main_text;
private TextView tv_main_showThink;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_text = (EditText) findViewById(R.id.et_main_uname);
tv_main_showThink = (TextView) findViewById(R.id.tv_main_showThink);
// String thinks=getIntent().getStringExtra("thinks");
// tv_main_showThink.setText(thinks);
}
//获取其他人设值过来的结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String thinks=data.getStringExtra("thinks");
tv_main_showThink.setText(thinks);
}
public void writeThink(View view){
String uname=et_main_text.getText().toString();
Intent intent=new Intent(this,ThinkActivity.class);
//传递基本数据类型
// intent.putExtra("uname",uname);
// intent.putExtra("uage",19);
//传递Bundle
// Bundle bundle=new Bundle();
// bundle.putString("uname",uname);
// bundle.putInt("uage",10);
// intent.putExtra("bundle",bundle);
//传递对象
// Person person=new Person(1,"明明",10);
//传递对象集合
Person person1=new Person(1,"求其",20);
Person person2=new Person(2,"微微",12);
Person person3=new Person(3,"琪琪",30);
ArrayList<Person> persons=new ArrayList<>();
persons.add(person1);
persons.add(person2);
persons.add(person3);
//传递对象
//intent.putExtra("person",person);
//传递对象集合
intent.putParcelableArrayListExtra("persons",persons);
// startActivity(intent);
//带有目的的跳转(请求码,结果码)
startActivityForResult(intent,0x23);
}
}</span>
第二个:
<span style="color:#000000;">public class ThinkActivity extends AppCompatActivity {
private TextView tv_think_showName;
private EditText et_think_someThink;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_think);
tv_think_showName = (TextView) findViewById(R.id.tv_think_showName);
et_think_someThink = (EditText) findViewById(R.id.et_think_someThink);
//获取第一个页面传递过来的数据
// String uname=getIntent().getStringExtra("uname");
//设值默认值
// int uage=getIntent().getIntExtra("uage",0);
//接收Bundle
// Bundle bundle=getIntent().getBundleExtra("bundle");
// String uname=bundle.getString("uname");
// int uage=bundle.getInt("uage");
// tv_think_showName.setText("你好:"+uname+" "+uage);
//接收对象
// Person person= (Person) getIntent().getSerializableExtra("person");
//运用安卓的序列化进行接收对象
// Person person=getIntent().getParcelableExtra("person");
// tv_think_showName.setTextSize(30);
// tv_think_showName.setText("你好:"+person.toString());
//接收对象集合
List<Person> persons=getIntent().getParcelableArrayListExtra("persons");
for (Person person : persons) {
tv_think_showName.setText(tv_think_showName.getText()+"\n"+person);
}
}
public void ok(View view){
String thinks=et_think_someThink.getText().toString();
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("thinks",thinks);
//设值结果
setResult(0x12,intent);
//自杀(干掉自己,干掉当前的Activity)
finish();
//startActivity(intent);
}
}</span>
<span style="color:#000000;">public class ThinkActivity extends AppCompatActivity {
private TextView tv_think_showName;
private EditText et_think_someThink;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_think);
tv_think_showName = (TextView) findViewById(R.id.tv_think_showName);
et_think_someThink = (EditText) findViewById(R.id.et_think_someThink);
//获取第一个页面传递过来的数据
// String uname=getIntent().getStringExtra("uname");
//设值默认值
// int uage=getIntent().getIntExtra("uage",0);
//接收Bundle
// Bundle bundle=getIntent().getBundleExtra("bundle");
// String uname=bundle.getString("uname");
// int uage=bundle.getInt("uage");
// tv_think_showName.setText("你好:"+uname+" "+uage);
//接收对象
// Person person= (Person) getIntent().getSerializableExtra("person");
//运用安卓的序列化进行接收对象
// Person person=getIntent().getParcelableExtra("person");
// tv_think_showName.setTextSize(30);
// tv_think_showName.setText("你好:"+person.toString());
//接收对象集合
List<Person> persons=getIntent().getParcelableArrayListExtra("persons");
for (Person person : persons) {
tv_think_showName.setText(tv_think_showName.getText()+"\n"+person);
}
}
public void ok(View view){
String thinks=et_think_someThink.getText().toString();
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("thinks",thinks);
//设值结果
setResult(0x12,intent);
//自杀(干掉自己,干掉当前的Activity)
finish();
//startActivity(intent);
}
}</span>