安卓界面间使用Intent、Intent+Bundle传递数据,自定义类序列化

1.说明

<1>界面数据传递有intent,Intent+bundle两种方式
<2>传递的数据类型又分为基本数据类型,基本数据类型列表,自定义类,自定义类列表四种
<3>序列化有Serializable和Parcelable两种方式。
<4>Serializable序列化只需要将类实现Serializable接口即可;Parcelable序列化类需要实现Parcelable接口,并且重写序列化、非序列化、描述三个方法
<5>Parcelable序列化实例类News在结尾(可参考)

2.1 传递基本类型数据

<1>Intent传递
(1)放:

Intent intent=new Intent(MainActivity.this,SecondActivity.class);
String M=“张三”;
intent.putExtra(“name”,M);
intent.putExtra(“age”,15);
startActivity(intent);

(2)取

Intent intent=getIntent();
Log.d(“177777”,“name=”+intent.getStringExtra(“name”)+“;age=”+intent.getIntExtra(“age”,0));

<2>Intent+Bundle传递
(1)放

Intent intent=new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle =new Bundle();
bundle.putString(“C”,“C”);
intent.putExtra(“bundle”,bundle);
startActivity(intent);

(2)取

Bundle bundle=getIntent().getBundleExtra(“bundle”);
Log.d(“177777”,“C=”+bundle.getString(“C”));

2.2传递基本数据类型List

<1>Intent传递
(1)放

Intent intent=new Intent(MainActivity.this,SecondActivity.class);
ArrayList M=new ArrayList<>();
M.add(“15”);M.add(“17”);M.add(“25”);
intent.putStringArrayListExtra(“M”,M);
startActivity(intent);

(2)取

Intent intent=getIntent();
Log.d(“177777”,“M=”+intent.getStringArrayListExtra(“M”));

<2>Intent+Bundle传递
(1)放

ArrayList M=new ArrayList<>();
M.add(“15”);M.add(“17”);M.add(“25”);
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle =new Bundle();
bundle.putStringArrayList(“list”,M);
intent.putExtra(“bundle”,bundle);
startActivity(intent);

(2)取

Bundle bundle=getIntent().getBundleExtra(“bundle”);
Log.d(“177777”,“list=”+bundle.getStringArrayList(“list”));

2.3 传递自定义对象News

<1>Intent传递
[1]News类实现Serializable接口
(1)放

Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“news”,new News(“NNNN”));
startActivity(intent);

(2)取

Intent intent=getIntent();
Log.d(“177777”,“News=”+intent.getSerializableExtra(“news”));

[2]News类实现Parcelable接口
(1)放

News news=new News(“BBB”);
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“Parcelble”,news);
startActivity(intent);

(2)取

Log.d(“177777”,“news=”+getIntent().getParcelableExtra(“Parcelble”));

<2>Intent+Bundle传递
[1]News类实现Serializable接口
(1)放

News news=new News(“NNNNN”);
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle =new Bundle();
bundle.putSerializable(“news”,news);
intent.putExtra(“bundle”,bundle);
startActivity(intent);

(2)取

Bundle bundle=getIntent().getBundleExtra(“bundle”);
Log.d(“177777”,“news=”+bundle.getSerializable(“news”));

[2]News类实现Parcelable接口
(1)放

News news=new News(“BBB”);
Bundle bundle =new Bundle();
bundle.putParcelable(“Parcelble”,news);
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“bundle”,bundle);
startActivity(intent);

(2)取

Bundle bundle=getIntent().getBundleExtra(“bundle”);
Log.d(“177777”,“news=”+bundle.getParcelable(“Parcelble”));

2.4 传递自定义对象List

<1>Intent传递
[1]News类实现Serializable接口
(1)放

ArrayList M=new ArrayList<>();
M.add(new News(“1”));M.add(new News(“2”));M.add(new News(“3”));
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“list”,M);
startActivity(intent);

(2)取

Log.d(“177777”,“list=”+getIntent().getSerializableExtra(“list”));

[2]News类实现Parcelable接口
(1)放

ArrayList M=new ArrayList<>();
M.add(new News(“1”));M.add(new News(“2”));M.add(new News(“3”));
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putParcelableArrayListExtra(“list”,M);
startActivity(intent);

(2)取

Log.d(“177777”,“list=”+getIntent().getParcelableArrayListExtra(“list”));

<2>Intent+Bundle传递
[1]News类实现Serializable接口
(1)放

ArrayList M=new ArrayList<>();
M.add(new News(“1”));M.add(new News(“2”));M.add(new News(“3”));
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle =new Bundle();
bundle.putSerializable(“news”,M);
intent.putExtra(“bundle”,bundle);
startActivity(intent);

(2)取

Bundle bundle=getIntent().getBundleExtra(“bundle”);
Log.d(“177777”,“news=”+bundle.getSerializable(“news”));

[2]News类实现Parcelable接口
(1)放

ArrayList M=new ArrayList<>();
M.add(new News(“1”));M.add(new News(“2”));M.add(new News(“3”));
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle=new Bundle();
bundle.putParcelableArrayList(“list”,M);
intent.putExtra(“bundle”,bundle);
startActivity(intent);

(2)取

Bundle bundle=getIntent().getBundleExtra(“bundle”);
Log.d(“177777”,“list=”+bundle.getParcelableArrayList(“list”));

2.5 Parcelable序列化示例类News

package com.example.xianchengtest;

import android.os.Parcel;
import android.os.Parcelable;

import java.io.Serializable;

public class News implements Parcelable {
    public String content;

    public String getContent() {
        return content;
    }


    public News(String content) {
        this.content = content;
    }

    protected News(Parcel in) {
        content = in.readString();
    }


    /*
     * 序列化
     * */
    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(content);
        // 序列化自定义类
        // parcel.writeParcelable("自定义类");
    }


    /*
    * 反序列化
    * */
    public static final Creator<News> CREATOR = new Creator<News>() {
        @Override
        public News createFromParcel(Parcel in) {
            return new News(in);
        }

        @Override
        public News[] newArray(int size) {
            return new News[size];
        }
    };

    /*
    * 描述
    * */
    @Override
    public int describeContents() {
        return 0;
    }

}

转载请注明出处

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值