Android ArrayList pass as parcelable

后端 未结 3 1090
长发绾君心
长发绾君心 2020-12-03 07:08

Code now modified to reflect the accepted solution.

This now serves as a working example of how to pass a custom ArrayList into a DialogFrag

相关标签:
3条回答
  • 2020-12-03 07:43

    I know this question is rather old but since I originally came here looking for answers, I wanted to share my experience.

    Yes, you need to implement Parcelable for your Locality class but that is it.

    If your LocalityList is ONLY a wrapper for ArrayList, then you do not need it.

    Just use the putParcelableArrayList method.

    ArrayList<Locality> localities = new ArrayList<Locality>;
    ...
    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList(KEY_LOCALITY_LIST, localities);
    fragmentInstance.setArguments(bundle);
    
    return fragmentInstance;
    

    And retrieve it using...

    localities = savedInstanceState.getParcelableArrayList(KEY_LOCALITY_LIST);
    

    So, unless you need the custom ArrayList for some other reason, you can avoid doing any of that extra work and only implement Parcelable for your Locality class.

    0 讨论(0)
  • 2020-12-03 07:47

    Yes, make Locality class itself Parcelable, and don't forgot to initialize

    ArrayList<Locality> mList= new ArrayList<Locality>();
    
    0 讨论(0)
  • 2020-12-03 07:53

    The trick i normally use is to parse the list to Json using Gson (from google). On the other side i just parte the string in Json back to a new list.

    Never noticed any lag.

    0 讨论(0)
提交回复
热议问题