Android 之数据传递小结
2024-09-21 06:47:41
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对象传递复杂数据(引入java.util.*)
有时候传递如ArrayList之类复杂些的数据,这种原理是和上面一种是一样的,只是在传参数前,要用新增加一个List将对象包起来。如下:
[1]发送页面:
//传递复杂些的参数
Map<String, Object> map = new HashMap<String, Object>();
map.put("1", "传值");
map.put("2", "成功");
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
list.add(map);
Intent intent = new Intent();
intent.setClass(MainActivity.this,SecondActivity.class);
Bundle bundle = new Bundle();
//须定义一个list用于在bundle中传递需要传递的ArrayList<Object>,这个是必须要的
ArrayList bundlelist = new ArrayList();
bundlelist.add(list);
bundle.putParcelableArrayList("list",bundlelist);
intent.putExtras(bundle);
startActivity(intent);
[2]接收页面:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView txt = (TextView)this.findViewById(R.id.txt); Bundle bundle = getIntent().getExtras();
ArrayList list = bundle.getParcelableArrayList("list");
//从List中将参数转回 List<Map<String, Object>>
List<Map<String, Object>> lists= (List<Map<String, Object>>)list.get(0);
String sResult = "";
for (Map<String, Object> m : lists)
{
for (String k : m.keySet())
{
sResult += "\r\n"+k + " : " + m.get(k);
}
}
txt.setText(sResult);
}
3、通过实现Serializable接口(引入java.util.*)
通过将数据序列化后,再将其传递出去。
(1)发送页面:
//通过Serializable接口传参数的例子
HashMap<String,String> hm = new HashMap<String,String>();
hm.put("1", "传值");
hm.put("2", "成功"); Bundle bundle = new Bundle();
bundle.putSerializable("serializable", hm);
Intent intent = new Intent();
intent.putExtras(bundleSerializable);
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
(2)接收页面:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView txt = (TextView)this.findViewById(R.id.txt); //接收参数
Bundle bundle = this.getIntent().getExtras();
//传HashMap
HashMap<String,String> hm = (HashMap<String,String>)bundle.getSerializable("serializable");
String sResult = "";
Iterator iter = hm.entrySet().iterator();
while(iter.hasNext())
{
Map.Entry entry = (Map.Entry)iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
sResult += (String)key;
sResult += (String)value;
}
txt.setText(sResult);
}
4、通过实现Parcelable接口
通过实现Parcelable接口,把要传的数据打包在里面,然后在接收端自己分解出来。这个是Android独有的,在其本身的源码中也用得很多,
效率要比Serializable相对要好。
(1)首先要定义一个类,用于 实现Parcelable接口
实现Parcelable
(2)发送页面
Intent intent = new Intent();
Person p = new Person();
p.mInt = 1;
p.mStr = "传值";
p.mMap = new HashMap<String,String>();
p.mMap.put("key", "value");
intent.putExtra("Parcelable", p);
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
(3)接收页面
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView txt = (TextView)this.findViewById(R.id.txt);
//接收参数
Intent intent = getIntent();
Person p = intent.getParcelableExtra("Parcelable");
String sResult = " mInt ="+ p.mInt +"\r\n mStr" + p.mStr + "\r\n mMap.size="+p.mMap.size(); txt.setText(sResult);
}
5、通过单例模式实现参数传递
(1)单例模式的特点就是可以保证系统中一个类有且只有一个实例。这样很容易就能实现,在A中设置参数,在B中直接访问了,是效率最高的。
package com.model;
import java.util.*; public class XclSingleton {
//单例模式实例
private static XclSingleton instance = null;
//synchronized 用于线程安全,防止多线程同时创建实例
public synchronized static XclSingleton getInstance(){
if(instance == null){
instance = new XclSingleton();
}
return instance;
} public final HashMap<String, Object> mMap;
public XclSingleton()
{
mMap = new HashMap<String,Object>();
} public void put(String key,Object value){
mMap.put(key,value);
} public Object get(String key)
{
return mMap.get(key);
}
}
单例模式
(2)发送页面
XclSingleton.getInstance().put("1", "传值");
XclSingleton.getInstance().put("2", "成功");
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class); startActivity(intent);
(3)接收页面
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView txt = (TextView)this.findViewById(R.id.txt);
//接收参数
HashMap<String,Object> map = XclSingleton.getInstance().mMap;
String sResult = "map.size() ="+map.size();
//遍历参数
Iterator iter = map.entrySet().iterator();
while(iter.hasNext())
{
Map.Entry entry = (Map.Entry)iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
sResult +="\r\n key----> "+(String)key;
sResult +="\r\n value----> "+(String)value;
}
txt.setText(sResult);
}
最新文章
- java.lang.OutOfMemoryError: PermGen space及其解决方法
- 如何在arcmap中调试addin或者插件
- windows API 创建临时文件
- request.get request.GET……
- Google加强版权保护
- 集合框架,ArrayList和Vector的区别,让arrayList线程安全的几种方案
- MVVM模式应用体会
- twisted(3)--再谈twisted
- PHP中实现在数据库中的增、删、查、改
- node.js 开发环境配置 和使用方式
- JavaJavaScript小问题系列之JSON解析
- BUAA软工第0次作业
- ubuntu 18.04 配置远程ssh/远程ftp/远程vnc登陆
- git 回退版本
- postman(五):在不同接口之间传递数据
- java基础-day16
- Vue.js开始第一个项目
- [转]一个简洁的 systemd 操作指南
- KMP 求next数组
- python 使用selenium和requests爬取页面数据