天天看點

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. import android.os.Parcel;  
  3. import android.os.Parcelable;  
  4. public class Student implements Parcelable {  
  5.     private String name;  
  6.     private int age;  
  7.     protected Student() {  
  8.     }  
  9.     public String getName() {  
  10.         return name;  
  11.     }  
  12.     public void setName(String name) {  
  13.         this.name = name;  
  14.     }  
  15.     public int getAge() {  
  16.         return age;  
  17.     }  
  18.     public void setAge(int age) {  
  19.         this.age = age;  
  20.     }  
  21.     protected Student(Parcel in) {  
  22.         name = in.readString();  
  23.         age = in.readInt();  
  24.     }  
  25.     public static final Creator<Student> CREATOR = new Creator<Student>() {  
  26.         @Override  
  27.         public Student createFromParcel(Parcel in) {  
  28.             return new Student(in);  
  29.         }  
  30.         @Override  
  31.         public Student[] newArray(int size) {  
  32.             return new Student[size];  
  33.         }  
  34.     };  
  35.     @Override  
  36.     public int describeContents() {  
  37.         return 0;  
  38.     }  
  39.     @Override  
  40.     public void writeToParcel(Parcel dest, int flags) {  
  41.         dest.writeString(name);  
  42.         dest.writeInt(age);  
  43.     }  
  44. }  

2.MainActivity.java 跳轉并傳遞List和Object資料

[java]  view plain  copy

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

3.StudentActivity.java 接收MainActivity傳遞過來的List的資料

[java]  view plain  copy

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

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.     <TextView  
  12.         android:id="@+id/tv"  
  13.         android:text="student"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content" />  
  16.     <TextView  
  17.         android:id="@+id/tv2"  
  18.         android:layout_below="@id/tv"  
  19.         android:layout_marginTop="20dp"  
  20.         android:text="student"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content" />  
  23. </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. <TextView  
  12.     android:id="@+id/tv"  
  13.     android:text="student"  
  14.     android:layout_width="wrap_content"  
  15.     android:layout_height="wrap_content" />  
  16. <TextView  
  17.     android:id="@+id/tv2"  
  18.     android:layout_below="@id/tv"  
  19.     android:layout_marginTop="20dp"  
  20.     android:text="student"  
  21.     android:layout_width="wrap_content"  
  22.     android:layout_height="wrap_content" />  
  23. </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.     <application  
  5.         android:allowBackup="true"  
  6.         android:icon="@mipmap/ic_launcher"  
  7.         android:label="@string/app_name"  
  8.         android:supportsRtl="true"  
  9.         android:theme="@style/AppTheme">  
  10.         <activity android:name=".MainActivity">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.         <activity android:name=".StudentActivity"></activity>  
  17.     </application>  
  18. </manifest>  

繼續閱讀