首页 > 移动平台 > 详细

Android 之数据传递小结

时间:2014-11-17 12:04:32      阅读:312      评论:0      收藏:0      [点我收藏+]

      Android开发中,在不同模块(如Activity)间经常会有各种各样的数据需要相互传递,常用的的有五种传递方式。它们各有利弊,有各自的应用场景。下面分别介绍一下:

1、 Intent对象传递简单数据

      Intent的Extra部分可以存储传递的数据,可以传送int, long, char等一些基础类型。

[1]传递页面:

Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();     //打包发送
bundle.putString("name","123");    //绑定参数
intent.putExtra("maps",bundle);
startActivity(intent);

[2]接收页面:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    Intent intent = this.getIntent();
    Bundle bundle = intent.getBundleExtra("maps");     //获取打包数据bundle
    String name = bundle.getString("name");     //取出需要的数据
    tv.setText(name);
    setContentView(tv);
}

或者

@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.second);
  TextView txt = (TextView)this.findViewById(R.id.txt);
  Intent intent = this.getIntent();
  Bundle bundle = intent.getBundleExtra("maps");     //获取打包数据bundle
  String name = bundle.getString("name");     //取出需要的数据
  txt.setText(name);
 }

2.、Intent对象传递复杂数据

      有时候我们想传递如ArrayList之类复杂些的数据,这种原理是和上面一种是一样的,只是在传参数前,要用新增加一个List将对象包起来。如下:

 

Android 之数据传递小结

原文:http://www.cnblogs.com/xinaixia/p/4103216.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!