借助Intent实现Android工程中Activity之间Java对象的传递——实现Serializable接口

        借助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;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.from);
		button = (Button)findViewById(R.id.ok_btn);
		button.setOnClickListener(this);
	}

	@Override
	public 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{

	@Override
	protected 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿老高

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值