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

來源:互聯網
上載者:User

標籤:

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

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

package com.ghj.vo;import java.io.Serializable;public class User implements Serializable{private static final long serialVersionUID = -8278907591742219230L;private String id;private String name;public User(String id, String name) {this.id = id;this.name = name;}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;}}

        代碼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.putSerializable("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  = (User) intent.getSerializableExtra("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>

        注意:採用傳遞對象實現Serializable介面的方式實現Java對象的傳遞,那麼所傳遞對象的類一定要序列化(比如上面User類實現了Serializable),否則會出現類似如下的異常:java.lang.ClassCastException: com.ghj.vo.User cannot be cast to java.io.Serializable

        【0分下載Demo

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

聯繫我們

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