Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何传递HashMap<;字符串,对象>;通过片段中的bundle参数?_Android_Object_Serialization_Hashmap - Fatal编程技术网

Android 如何传递HashMap<;字符串,对象>;通过片段中的bundle参数?

Android 如何传递HashMap<;字符串,对象>;通过片段中的bundle参数?,android,object,serialization,hashmap,Android,Object,Serialization,Hashmap,我试图通过参数传递HashMap,但在通过getSeralizable方法获取HashMap时,得到的是空的HashMap(我放置的HashMap不是空的&大小为11)。下面是我的代码片段 public static FirstPageFragment newInstance(boolean is_pending, HashMap<String, Object> result_map) { FirstPageFragment fragment = new FirstPageF

我试图通过参数传递HashMap,但在通过getSeralizable方法获取HashMap时,得到的是空的HashMap(我放置的HashMap不是空的&大小为11)。下面是我的代码片段

public static FirstPageFragment newInstance(boolean is_pending, HashMap<String, Object> result_map) {
    FirstPageFragment fragment = new FirstPageFragment();
    Bundle args = new Bundle();
    args.putBoolean(ARG_PARAM1, is_pending);
    args.putSerializable(ARG_PARAM2, result_map);
    fragment.setArguments(args);
    return fragment;
}

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        Bundle b=getArguments();
        isPending = b.getBoolean(ARG_PARAM1);
        mResultMap = (HashMap<String, Object>)b.getSerializable(ARG_PARAM2);
    }

}
publicstaticfirstpagefragmentnewinstance(布尔值为挂起,HashMap结果为映射){
FirstPageFragment=新的FirstPageFragment();
Bundle args=新Bundle();
args.putBoolean(ARG_参数1,正在挂起);
args.putSerializable(ARG_PARAM2,result_map);
fragment.setArguments(args);
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果(getArguments()!=null){
Bundle b=getArguments();
isPending=b.getBoolean(ARG_PARAM1);
mResultMap=(HashMap)b.getSerializable(ARG_PARAM2);
}
}
我们可以这样做吗?如果不能这样做,有没有其他方法可以做到这一点。

试试这个:

Bundle extras = new Bundle();
extras.putSerializable("YourHashMap",hashMap);
intent.putExtras(extras);
在另一项活动中

Bundle bundle = this.getIntent().getExtras();

if(bundle != null) {
   hashMap = bundle.getSerializable("YourHashMap");
}

哈希映射

扩展抽象地图实现地图,可克隆, 可序列化


您可以这样做,因为
HashMap
实现是可序列化的

使用
putExtra(字符串键,可序列化obj)
插入
HashMap
,在另一个活动中使用
getIntent().getserializablextra(字符串键)
,您需要将返回值转换为
HashMap

它在活动中工作。但是否可以将包设置为片段中的参数?@Anu