Intent传递Parcelable List对象
2017-01-19 18:27
716 查看
步骤:
1.首先对象要实现Parcelable接口
2.用Intent发送对象或者list,关键代码
bundle.putParcelable("student", stu);
bundle.putParcelableArrayList("student_list", list);
3.用Intent获取对象或者list ,关键代码
Student student = (Student) intent.getExtras().get("student");
ArrayList<Student> list = intent.getParcelableArrayListExtra("student_list");
下面给出完整代码示例:
1. Strudent.Java 实现Parcelable接口
[java] view
plain copy
package cn.getchance.testparcelable;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by chengyi on 15/11/5.
*/
public class Student implements Parcelable {
private String name;
private int age;
protected Student() {
}
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;
}
protected Student(Parcel in) {
name = in.readString();
age = in.readInt();
}
public static final Creator<Student> CREATOR = new Creator<Student>() {
@Override
public Student createFromParcel(Parcel in) {
return new Student(in);
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
}
2.MainActivity.java 跳转并传递List和Object数据
[java] view
plain copy
package cn.getchance.testparcelable;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Student stu = new Student();
stu.setAge(108);
stu.setName("s1");
Student stu2 = new Student();
stu2.setAge(109);
stu2.setName("s2");
Student stu3 = new Student();
stu3.setAge(110);
stu3.setName("s3");
ArrayList<Student> list = new ArrayList<Student>();
list.add(stu);
list.add(stu2);
list.add(stu3);
Intent i = new Intent(MainActivity.this, StudentActivity.class);
Bundle bundle = new Bundle();
//传递对象
bundle.putParcelable("student", stu);
//传递List ,这里注意只能传ArrayList
bundle.putParcelableArrayList("student_list", list);
i.putExtras(bundle);
MainActivity.this.startActivity(i);
}
});
}
}
3.StudentActivity.java 接收MainActivity传递过来的List的数据
[java] view
plain copy
package cn.getchance.testparcelable;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.ArrayList;
public class StudentActivity extends AppCompatActivity {
private TextView tv, tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student);
tv = (TextView) findViewById(R.id.tv);
tv2 = (TextView) findViewById(R.id.tv2);
Intent intent = getIntent();
if (intent != null) {
Bundle b = intent.getExtras();
Student student = (Student) b.get("student");
if (student != null) {
tv.setText(student.getName() + ":" + student.getAge());
}
//关键性代码,通过intent.getParcelableArrayListExtra方法获取list数据
ArrayList<Student> list = intent.getParcelableArrayListExtra("student_list");
if (list != null && list.size() > 0) {
String str = "";
for (Student s : list) {
str += s.getName() + " | ";
}
tv2.setText(str);
}
}
}
}
activity_student.xml
[html] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.getchance.testparcelable.StudentActivity">
<TextView
android:id="@+id/tv"
android:text="student"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv2"
android:layout_below="@id/tv"
android:layout_marginTop="20dp"
android:text="student"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
activity_mian.xml
[html] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.getchance.testparcelable.StudentActivity">
<TextView
android:id="@+id/tv"
android:text="student"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv2"
android:layout_below="@id/tv"
android:layout_marginTop="20dp"
android:text="student"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
AndroidManifest.xml
[html] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.getchance.testparcelable">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".StudentActivity"></activity>
</application>
</manifest>
1.首先对象要实现Parcelable接口
2.用Intent发送对象或者list,关键代码
bundle.putParcelable("student", stu);
bundle.putParcelableArrayList("student_list", list);
3.用Intent获取对象或者list ,关键代码
Student student = (Student) intent.getExtras().get("student");
ArrayList<Student> list = intent.getParcelableArrayListExtra("student_list");
下面给出完整代码示例:
1. Strudent.Java 实现Parcelable接口
[java] view
plain copy
package cn.getchance.testparcelable;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by chengyi on 15/11/5.
*/
public class Student implements Parcelable {
private String name;
private int age;
protected Student() {
}
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;
}
protected Student(Parcel in) {
name = in.readString();
age = in.readInt();
}
public static final Creator<Student> CREATOR = new Creator<Student>() {
@Override
public Student createFromParcel(Parcel in) {
return new Student(in);
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
}
2.MainActivity.java 跳转并传递List和Object数据
[java] view
plain copy
package cn.getchance.testparcelable;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Student stu = new Student();
stu.setAge(108);
stu.setName("s1");
Student stu2 = new Student();
stu2.setAge(109);
stu2.setName("s2");
Student stu3 = new Student();
stu3.setAge(110);
stu3.setName("s3");
ArrayList<Student> list = new ArrayList<Student>();
list.add(stu);
list.add(stu2);
list.add(stu3);
Intent i = new Intent(MainActivity.this, StudentActivity.class);
Bundle bundle = new Bundle();
//传递对象
bundle.putParcelable("student", stu);
//传递List ,这里注意只能传ArrayList
bundle.putParcelableArrayList("student_list", list);
i.putExtras(bundle);
MainActivity.this.startActivity(i);
}
});
}
}
3.StudentActivity.java 接收MainActivity传递过来的List的数据
[java] view
plain copy
package cn.getchance.testparcelable;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.ArrayList;
public class StudentActivity extends AppCompatActivity {
private TextView tv, tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student);
tv = (TextView) findViewById(R.id.tv);
tv2 = (TextView) findViewById(R.id.tv2);
Intent intent = getIntent();
if (intent != null) {
Bundle b = intent.getExtras();
Student student = (Student) b.get("student");
if (student != null) {
tv.setText(student.getName() + ":" + student.getAge());
}
//关键性代码,通过intent.getParcelableArrayListExtra方法获取list数据
ArrayList<Student> list = intent.getParcelableArrayListExtra("student_list");
if (list != null && list.size() > 0) {
String str = "";
for (Student s : list) {
str += s.getName() + " | ";
}
tv2.setText(str);
}
}
}
}
activity_student.xml
[html] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.getchance.testparcelable.StudentActivity">
<TextView
android:id="@+id/tv"
android:text="student"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv2"
android:layout_below="@id/tv"
android:layout_marginTop="20dp"
android:text="student"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
activity_mian.xml
[html] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.getchance.testparcelable.StudentActivity">
<TextView
android:id="@+id/tv"
android:text="student"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv2"
android:layout_below="@id/tv"
android:layout_marginTop="20dp"
android:text="student"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
AndroidManifest.xml
[html] view
plain copy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.getchance.testparcelable">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".StudentActivity"></activity>
</application>
</manifest>
相关文章推荐
- Intent传递Parcelable List对象
- Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!
- Android中Intent传递对象的两种方法(Serializable,Parcelable)!
- android intent 传递list或者对象
- Android高手进阶教程(十六)---Android中Intent传递对象的两种方法(Serializable,Parcelable)!
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
- Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!
- Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!
- Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
- Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
- 在两个activity之间使用intent传递list类型的对象
- Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]