跨Android片段传输哈希映射

时间:2014-11-25 05:37:44

标签: java android

说我有:

private Map<Integer,String> prices = new HashMap<Integer,String>();
private Map<Integer,String> titles = new HashMap<Integer,String>();
private Map<Integer,String> descriptions = new HashMap<Integer,String>();
private Map<Integer,String[]> pictures = new HashMap<Integer,String[]>();

我想通过捆绑包将这些转移到另一个片段:

Bundle bundle = new Bundle();
bundle.put  //best way?
fragment.setArguments(bundle);
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
    .replace(R.id.frame_container, fragment).commit();
}

最有效/最简单的方法是什么?

1 个答案:

答案 0 :(得分:1)

在片段类中创建这些HashMap的setter。然后用它将这些参数传递给片段。

fragment.setHashMaps(Map<Integer,String> prices, Map<Integer,String> titles, Map<Integer,String> descriptions, Map<Integer,String[]> pictures) {
    this.prices = prices;
    this.titles= titles;
    this.descriptions = descriptions;
    this.pictures = pictures;
}