search

Home  >  Q&A  >  body text

android - 请问Bundle中如何存入ArrayList<ArrayList<Strng>>?

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

大家讲道理大家讲道理2772 days ago729

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 09:07:39

    Convert to json

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:07:39

    bundle.putSerializable("key",lists);

    reply
    0
  • 黄舟

    黄舟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 StringArrayList 都实现了Serializable接口, 但是Serializable序列化的性能是比较低的, ArrayList<ArrayList<String>> & ArrayList<String> & String can be discussed further.

    reply
    0
  • Cancelreply