posts - 609,  comments - 13,  views - 64万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
1.Intent对象携带简单数据
利用Intent的Extra部分来存储我们想要传递的数据,可以传送int, long, char等一些基础类型,对复杂的对象就无能为力了。
//传递些简单的参数
Intent intentSimple = new Intent();
intentSimple.setClass(MainActivity.this,SimpleActivity.class);
Bundle bundleSimple = new Bundle();
bundleSimple.putString("usr", "xcl");
bundleSimple.putString("pwd", "zj");
intentSimple.putExtras(bundleSimple);
startActivity(intentSimple);

接收参数
Bundle bunde = this.getIntent().getExtras();
String eml = bunde.getString("usr");
String pwd = bunde.getString("pwd");

2. 利用Intent对象携带如ArrayList之类复杂些的数据
这种原理是和上面一种是一样的,只是要注意下。 在传参数前,要用新增加一个List将对象包起来。
设置参数
//传递复杂些的参数
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("key1", "value1");
map1.put("key2", "value2");
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
list.add(map1);

Intent intent = new Intent();
intent.setClass(MainActivity.this,ComplexActivity.class);
Bundle bundle = new Bundle();
//须定义一个list用于在budnle中传递需要传递的ArrayList<Object>,这个是必须要的
ArrayList bundlelist = new ArrayList();
bundlelist.add(list);
bundle.putParcelableArrayList("list",bundlelist);
intent.putExtras(bundle);
startActivity(intent);

//接收参数
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);
  }
}

3. 通过实现Serializable接口
设置参数
利用Java语言本身的特性,通过将数据序列化后,再将其传递出去。

复制代码
// 使用 LinkedHashMap 传递,接收的时候要用 HashMap 强制转换。
                    LinkedHashMap<String,String> map1 = new LinkedHashMap<>();
                    map1.put("key11", "value11");
                    map1.put("key22", "value22");

//                    HashMap<String, String> map2 = new HashMap<String, String>();
//                    map2.put("key1", "value1");
//                    map2.put("key2", "value2");

                    Bundle bundleSerializable = new Bundle();
                    bundleSerializable.putSerializable("serializable", map1);
                    Intent intentSerializable = new Intent();
                    intentSerializable.putExtras(bundleSerializable);
                    intentSerializable.setClass(MainActivity.this, TestFormActivity.class);
                    startActivity(intentSerializable);
复制代码

接收参数

复制代码
//接收参数
        Bundle bundle = this.getIntent().getExtras();
        //如果传 LinkedHashMap,则bundle.getSerializable转换时会报ClassCastException,需要用HashMap转化。
        //传HashMap倒没有问题。
        HashMap<String, String> map = (HashMap<String, String>) bundle.getSerializable("serializable");
        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;
        }
        System.out.println("接收参数:\n" + sResult);
复制代码

 参考:https://blog.csdn.net/jiangjie4558/article/details/41083827/
https://zhuanlan.zhihu.com/p/584319251?utm_id=0

posted on   邢帅杰  阅读(548)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2017-06-29 WCF的例子
点击右上角即可分享
微信分享提示