藉助Intent實現Android工程中Activity之間Java對象的傳遞——實現Parcelable介面

來源:互聯網
上載者:User

標籤:

        藉助Intent實現Android工程中Activity之間Java對象的傳遞有兩種方式:一種所傳遞對象實現了Serializable介面;另一種是所傳遞對象實現了Parcelable介面,本部落格總結傳遞對象實現Parcelable介面的情況下如何?Java對象傳遞:

        代碼1、添加名為“User.java”的檔案:

package com.ghj.vo;import android.os.Parcel;import android.os.Parcelable;public class User implements Parcelable{private String id;private String name;public User(String id, String name) {this.id = id;this.name = name;}/** * 實現Parcelable介面的writeToParcel方法,將需要傳遞的資料寫入Parcel“容器”中,以便從 Parcel“容器”擷取資料。 */@Overridepublic void writeToParcel(Parcel out, int flags) {out.writeString(id);        out.writeString(name);}/** * 執行個體化靜態內部對象CREATOR實現介面Parcelable.Creator。注意:其中public static final一個都不能少,內部對象CREATOR的名稱不能改變且必須全部大寫。 */public static final Creator<User> CREATOR = new Creator<User>(){        @Override        public User[] newArray(int size){            return new User[size];        }                /**         * 從Parcel“容器”中讀取傳遞資料值,並封裝成單個User實體         */        @Override        public User createFromParcel(Parcel in){            return new User(in.readString(), in.readString());        }    };public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}/** * 描述內容,一般返回0即可。 */@Overridepublic int describeContents() {return 0;}}

        說明:上面User類通過實現Parcelable介面的writeToParcel方法將需要傳遞的資料封裝成Parcel對象,再通過內部實作類別實現Parcelable.Creator介面的createFromParcel方法將資料從Parcel對象中擷取出來並再次封裝成User對象。值得注意的是將傳遞的資料寫入Parcel中的順序(參見實現writeToParcel方法時)要和從Parcel中擷取資料的順序(參見實現createFromParcel方法時)一致。

        代碼2、添加名為“FromActivity.java”的檔案:

package com.ghj.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.ghj.vo.User;public class FromActivity extends Activity implements OnClickListener {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.from);button = (Button)findViewById(R.id.ok_btn);button.setOnClickListener(this);}@Overridepublic void onClick(View view) {Intent intent  = new Intent(FromActivity.this, ToActivity.class);Bundle bundle = new Bundle();User user = new User("56862586-108e-4749-93ca-440cd576ba92","王翔");    bundle.putParcelable("user", user);    intent.putExtras(bundle);    startActivity(intent);}}

        代碼3、添加名為“ToActivity.java”的檔案:

package com.ghj.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;import com.ghj.vo.User;public class ToActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.to);TextView textView = (TextView)findViewById(R.id.show_user_info);Intent intent = getIntent();User user  = intent.getParcelableExtra("user");textView.setText(user.getId() + ":" + user.getName());}}

        代碼4、添加名為“from.xml”的檔案:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <Button        android:id="@+id/ok_btn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:layout_margin="10dp"        android:text="傳遞Java對象" /></RelativeLayout>

        代碼5、添加名為“to.xml”的檔案:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:id="@+id/show_user_info"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"/></RelativeLayout>
        【0分下載Demo】

藉助Intent實現Android工程中Activity之間Java對象的傳遞——實現Parcelable介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.