標籤:
藉助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介面