android - 请问Bundle中如何存入ArrayList<ArrayList<Strng>>?
大家讲道理
大家讲道理 2017-04-18 09:05:39
0
3
738

Fragment构造中需要传入ArrayList<ArrayList<Strng>>,请问Bundle中如何存入?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(3)
巴扎黑

Convert to json

黄舟

bundle.putSerializable("key",lists);

黄舟

The method mentioned by @idisfkj is simple and violent. In addition, you can also split the parameters and pass in part of the code as follows:

public static final String ARG_TEST_PREFIX = "list.";

public static TestFragment newIntance(ArrayList<ArrayList<String>> lists) {
    Bundle args = new Bundle();
    if (lists != null) {
        int i = 0;
        for(ArrayList<String> list: lists) {
            args.putStringArrayList(ARG_TEST_PREFIX + (i++), list);
        }
    }

    TestFragment f = new TestFragment();
    f.setArguments(args);
    return f;
}

The code for obtaining parameters is as follows:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    ArrayList<ArrayList<String>> lists = new ArrayList<>();
    Bundle args = getArguments();
    for(int i = 0;; i++) {
        String key = ARG_TEST_PREFIX + i;
        if (!args.containsKey(key)) {
            break;
        }
        
        ArrayList<String> list = lists.getStringArrayList(key);
        lists.add(list);
    }
}

p.s. The serialization performance difference between StringArrayList 都实现了Serializable接口, 但是Serializable序列化的性能是比较低的, ArrayList<ArrayList<String>> & ArrayList<String> & String can be discussed further.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template