android - 使用 Serializable 方法将 HashMap(String,Boolean) 传递给 nextActivity-Android
问题描述
我有一个要传递给下一个活动的 Hashmap,所有答案都显示此方法-:
Intent intent=new Intent(MainActivity.this,NextActivity.class);
Bundle extras = new Bundle();
extras.putSerializable("YourHashMap",hashMap);
intent.putExtras(extras);
startActivity(intent);
但它显示了错误的第二个参数,它需要 Serializable 而不是 Map String,Boolean>
我什至给了这个基本的尝试-:
intent.putExtra("myMap",myMap);
但它说它无法解决方法
解决方案
试试这个
// pass HashMap from one Activity
Intent intent = new Intent(this, AboutActivity.class);
HashMap<String, Boolean> map = new HashMap<>();
map.put("var",true);
Bundle bun = new Bundle();
bun.putSerializable("map", map);
intent.putExtra("bundle",bun);
startActivity(intent);
overridePendingTransition(R.anim.slide_from_right, R.anim.nothing);
// get HashMap from another activity
Bundle bundle = getIntent().getBundleExtra("bundle");
if(bundle != null){
HashMap<String,Boolean> map = (HashMap<String, Boolean>) bundle.getSerializable("map");
if(map != null){
Log.e("bundle",map.get("var")+"");
}
}
推荐阅读
- c# - 多次添加同一成员与添加卡 trello 但不起作用
- javascript - 使用JS而不是Jquery获取具有特定ID的最后一个孩子
- java - Java树,递归返回父节点
- angular - ngOnChanges 没有触发
- javascript - Web Speech API 支持输入文本、选择、复选框和输入文本类型 numeric 还有吗?
- fabricjs - 由于“最大纹理大小”,FabricJS 无法将过滤器应用于大图像
- ios - Mapkit 上的图钉未显示
- design-patterns - 将多个服务器操作作为单个事务执行的设计模式
- solr - solr 文本搜索中的加权项
- input - IE Edge,溢出:自动(或任何滚动)使“文件”类型的输入元素不可用