有人能帮我通过意向发送hashmap
并在另一个活动中接收它吗?第一个参数是字符串,第二个参数是一个字符串列表。找不到任何人在努力或试图通过意向发送它,就像我尝试的那样。
Map<String, List<String>> map = new hashmap<>();
您必须使用Serializable并通过intent传递映射。数据发送器的代码示例如下:
Map map = new HashMap<String,List<String>>();
List<String> l1 = new ArrayList();
l1.add("HEllo");
l1.add("John");
l1.add("Michael");
l1.add("Jessy");
map.put("Names" , l1);
Intent intent = new Intent("CurrentActivityName".this, "DestinationActivityName".class);
intent.putExtra("Map",(Serializable) map);
startActivity(intent);
接收器代码:
Map map = new HashMap<String,List>();
map = (Map) getIntent().getSerializableExtra("Map");
现在,您可以使用名为map的变量访问数据。
创建一个实现Serializable的模型类
public class DataWrapper implements Serializable {
private Map map;
public DataWrapper(Map dataMap) {
this.map= dataMap;
}
public Map getData() {
return this.map;
}
}
对于碎片
Fragmentt recent = new Fragmentt();
Bundle bundle = new Bundle();
Map m = new HashMap<>();
m.put("data", data);
bundle.putSerializable("Data", new DataWrapper(m));
recent.setArguments(bundle);
在下一个片段上接收数据
DataWrapper dataWrapper = (DataWrapper) bundle.getSerializable("Data");
map = dataWrapper.getData();
对于活动
Intent intent = new Intent(this, Activity.class);
Map map = new HashMap<>();
map.put("Data", data);
intent.putExtra("Data", new DataWrapper(map));
startActivity(intent);
接收下一个活动的数据
Map map;
DataWrapper dataWrapper = (DataWrapper) getIntent().getSerializableExtra("Data");
map = dataWrapper.getData();