【问题标题】:How to send Map<String, List<String>> through a intent如何通过意图发送 Map<String, List<String>>
【发布时间】:2019-04-09 13:26:08
【问题描述】:

任何人都可以帮助我通过意图发送hashmap 并在其他活动中接收它第一个参数是字符串,第二个参数是字符串列表找不到任何正在努力或试图通过意图发送此消息的人我也在尝试的方式

Map<String, List<String>> map = new hashmap<>();

【问题讨论】:

标签: java android hashmap


【解决方案1】:

创建一个实现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();

【讨论】:

    【解决方案2】:

    您必须使用 Serializable 并通过意图传递地图。 数据发送器的代码示例如下:

    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");
    

    现在您可以使用名为 ma​​p 的变量访问数据。

    【讨论】:

      猜你喜欢
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 2018-09-22
      • 2020-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多