Fragment构造中需要传入ArrayList<ArrayList<Strng>>,请问Bundle中如何存入?
黄舟2017-04-18 09:07:39
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 String
和 ArrayList
都实现了Serializable
接口, 但是Serializable
序列化的性能是比较低的, ArrayList<ArrayList<String>>
& ArrayList<String>
& String
can be discussed further.