android intent 傳資料

來源:互聯網
上載者:User

標籤:

1. 基礎資料型別 (Elementary Data Type)

Intent intent = new Intent();   intent.setClass(activity1.this, activity2.class);   //描述起點和目標   Bundle bundle = new Bundle();                           //建立Bundle對象   bundle.putString("key", "封裝的資料");     //裝入資料   intent.putExtras(bundle);                                //把Bundle塞入Intent裡面   startActivity(intent); 

2. 傳對象的兩種方式 java.io.Serializable和android.os.Parcelable
1. android.os.Parcelable(android推薦使用)
1).被傳遞的類對象需要實現parcelable介面

public class Employee implements Parcelable{    public String id;    public String name;    public String dept;    public String idcard;    public String statusInt;    public String status;    public String mobile;    public String sex;    public String sexInt;    public String address;    public String avatar;    public String education;    public String birthday;    public String age;    public String dept_name;    public String imageUrl;    public String manager;    public String score;        public static final Parcelable.Creator<Employee> CREATOR = new Creator<Employee>() {          public Employee createFromParcel(Parcel source) {              Employee employee = new Employee();              employee.name = source.readString();              employee.age = source.readString();            employee.dept_name = source.readString();             employee.sex = source.readString();             employee.status = source.readString();             employee.manager = source.readString();            employee.score = source.readString();            employee.imageUrl = source.readString();            return employee;          }          public Employee[] newArray(int size) {              return new Employee[size];          }      };      @Override    public int describeContents() {        // TODO Auto-generated method stub        return 0;    }    @Override    public void writeToParcel(Parcel parcel, int arg1) {        parcel.writeString(name);        parcel.writeString(age);        parcel.writeString(dept_name);        parcel.writeString(sex);        parcel.writeString(status);                parcel.writeString(manager);        parcel.writeString(score);        parcel.writeString(imageUrl);    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

2)傳遞對象代碼

Intent intent = new Intent(mContext, AnotherActivity.class);                    Bundle mEmployeeBundle = new Bundle();                    mEmployeeBundle.putParcelable(EMPLOYEE_PAR_KEY, mEmployees.get(position));                    intent.putExtras(mEmployeeBundle);                    startActivity(intent);

3) AnotherActivity中取值

mEmployee = (Employee)getIntent().getExtras().getParcelable(HomeFragment.EMPLOYEE_PAR_KEY);Log.d(TAG, "employee name :"+mEmployee.name);

2. java.io.Serializable

1)類對象

public class Ser implements Serializable {    /**     * serialVersionUID的作用是在修改實體類後,可以正常的序列化和還原序列化,在此不多說,大家可以Google百度下。     */    private static final long serialVersionUID = 123456789090L;    private String name;    private int age;}

2)傳值及取值

bundle.putSerializable(SER_KEY, new Ser());  intent.putExtras(bundle);  startActivity(intent);  
pSer = (Ser) getIntent().getSerializableExtra(SER_KEY); //another activity 取值 

 

android intent 傳資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.