Intent传递Parcelable List对象

步骤:

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
  1. package cn.getchance.testparcelable;  
  2.   
  3. import android.os.Parcel;  
  4. import android.os.Parcelable;  
  5.   
  6. /** 
  7.  * Created by chengyi on 15/11/5. 
  8.  */  
  9. public class Student implements Parcelable {  
  10.   
  11.   
  12.     private String name;  
  13.     private int age;  
  14.   
  15.     protected Student() {  
  16.     }  
  17.   
  18.     public String getName() {  
  19.         return name;  
  20.     }  
  21.   
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.   
  26.     public int getAge() {  
  27.         return age;  
  28.     }  
  29.   
  30.     public void setAge(int age) {  
  31.         this.age = age;  
  32.     }  
  33.   
  34.     protected Student(Parcel in) {  
  35.         name = in.readString();  
  36.         age = in.readInt();  
  37.     }  
  38.   
  39.     public static final Creator<Student> CREATOR = new Creator<Student>() {  
  40.         @Override  
  41.         public Student createFromParcel(Parcel in) {  
  42.             return new Student(in);  
  43.         }  
  44.   
  45.         @Override  
  46.         public Student[] newArray(int size) {  
  47.             return new Student[size];  
  48.         }  
  49.     };  
  50.   
  51.     @Override  
  52.     public int describeContents() {  
  53.         return 0;  
  54.     }  
  55.   
  56.     @Override  
  57.     public void writeToParcel(Parcel dest, int flags) {  
  58.         dest.writeString(name);  
  59.         dest.writeInt(age);  
  60.     }  
  61. }  

2.MainActivity.java 跳转并传递List和Object数据

[java]  view plain  copy
  1. package cn.getchance.testparcelable;  
  2.   
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8.   
  9. import java.util.ArrayList;  
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.   
  13.     private Button btn;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         btn = (Button) findViewById(R.id.btn);  
  20.         btn.setOnClickListener(new View.OnClickListener() {  
  21.             @Override  
  22.             public void onClick(View v) {  
  23.                 Student stu = new Student();  
  24.                 stu.setAge(108);  
  25.                 stu.setName("s1");  
  26.   
  27.                 Student stu2 = new Student();  
  28.                 stu2.setAge(109);  
  29.                 stu2.setName("s2");  
  30.   
  31.   
  32.                 Student stu3 = new Student();  
  33.                 stu3.setAge(110);  
  34.                 stu3.setName("s3");  
  35.                 ArrayList<Student> list = new ArrayList<Student>();  
  36.                 list.add(stu);  
  37.                 list.add(stu2);  
  38.                 list.add(stu3);  
  39.   
  40.                 Intent i = new Intent(MainActivity.this, StudentActivity.class);  
  41.                 Bundle bundle = new Bundle();  
  42.   
  43.                 //传递对象  
  44.                 bundle.putParcelable("student", stu);  
  45.   
  46.                 //传递List ,这里注意只能传ArrayList  
  47.                 bundle.putParcelableArrayList("student_list", list);  
  48.                 i.putExtras(bundle);  
  49.                 MainActivity.this.startActivity(i);  
  50.             }  
  51.         });  
  52.     }  
  53. }  


3.StudentActivity.java 接收MainActivity传递过来的List的数据

[java]  view plain  copy
  1. package cn.getchance.testparcelable;  
  2.   
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.widget.TextView;  
  7.   
  8. import java.util.ArrayList;  
  9.   
  10. public class StudentActivity extends AppCompatActivity {  
  11.   
  12.     private TextView tv, tv2;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_student);  
  18.         tv = (TextView) findViewById(R.id.tv);  
  19.         tv2 = (TextView) findViewById(R.id.tv2);  
  20.         Intent intent = getIntent();  
  21.         if (intent != null) {  
  22.             Bundle b = intent.getExtras();  
  23.             Student student = (Student) b.get("student");  
  24.             if (student != null) {  
  25.                 tv.setText(student.getName() + ":" + student.getAge());  
  26.             }  
  27.   
  28.             //关键性代码,通过intent.getParcelableArrayListExtra方法获取list数据  
  29.             ArrayList<Student> list = intent.getParcelableArrayListExtra("student_list");  
  30.             if (list != null && list.size() > 0) {  
  31.                 String str = "";  
  32.                 for (Student s : list) {  
  33.                     str += s.getName() + " | ";  
  34.                 }  
  35.                 tv2.setText(str);  
  36.             }  
  37.         }  
  38.     }  
  39. }  

activity_student.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="cn.getchance.testparcelable.StudentActivity">  
  11.   
  12.   
  13.     <TextView  
  14.         android:id="@+id/tv"  
  15.         android:text="student"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content" />  
  18.   
  19.     <TextView  
  20.         android:id="@+id/tv2"  
  21.         android:layout_below="@id/tv"  
  22.         android:layout_marginTop="20dp"  
  23.         android:text="student"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content" />  
  26. </RelativeLayout>  

activity_mian.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. xmlns:tools="http://schemas.android.com/tools"  
  4. android:layout_width="match_parent"  
  5. android:layout_height="match_parent"  
  6. android:paddingBottom="@dimen/activity_vertical_margin"  
  7. android:paddingLeft="@dimen/activity_horizontal_margin"  
  8. android:paddingRight="@dimen/activity_horizontal_margin"  
  9. android:paddingTop="@dimen/activity_vertical_margin"  
  10. tools:context="cn.getchance.testparcelable.StudentActivity">  
  11.   
  12.   
  13. <TextView  
  14.     android:id="@+id/tv"  
  15.     android:text="student"  
  16.     android:layout_width="wrap_content"  
  17.     android:layout_height="wrap_content" />  
  18.   
  19. <TextView  
  20.     android:id="@+id/tv2"  
  21.     android:layout_below="@id/tv"  
  22.     android:layout_marginTop="20dp"  
  23.     android:text="student"  
  24.     android:layout_width="wrap_content"  
  25.     android:layout_height="wrap_content" />  
  26. </RelativeLayout>  


AndroidManifest.xml
[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="cn.getchance.testparcelable">  
  4.   
  5.     <application  
  6.         android:allowBackup="true"  
  7.         android:icon="@mipmap/ic_launcher"  
  8.         android:label="@string/app_name"  
  9.         android:supportsRtl="true"  
  10.         android:theme="@style/AppTheme">  
  11.         <activity android:name=".MainActivity">  
  12.             <intent-filter>  
  13.                 <action android:name="android.intent.action.MAIN" />  
  14.   
  15.                 <category android:name="android.intent.category.LAUNCHER" />  
  16.             </intent-filter>  
  17.         </activity>  
  18.         <activity android:name=".StudentActivity"></activity>  
  19.     </application>  
  20.   
  21. </manifest>  

发布了134 篇原创文章 · 获赞 26 · 访问量 77万+

猜你喜欢

转载自blog.csdn.net/yw1688/article/details/54618693