Passing ArrayList between intents loses data

妖精的绣舞 提交于 2019-12-20 07:29:30

问题


I am passing ArrayList<Custom implements Parcelable> myList to an Intent. Both of the following ways seem to work fine with putting the ArrayList into the new Intent.

        results.putParcelableArrayListExtra("list", myList);
        results.putExtra("list", myList);

When I check mIntent/mExtras/mMap/table it is everything there. But in the onCreate method of the intent some of that data seems to be lost. I am getting the ArrayList then with myList = (ArrayList<Custom>) this.getIntent().getParcelableArrayListExtra("list");

For example the list contains five items[a], [b], [c], [d] and [e]:

put...   get...
[a]  ->  [a]
[b]  ->  null
[c]  ->  [b]
[d]  ->  null
[e]  ->  [c]

It seems that every second item in the new list is not intended and takes up one place that is missing at the end. Can you tell me what I am doing wrong?


回答1:


try this my friend:

intent.putParcelableArrayListExtra("tag",yourObjectImplementsParceable);

xxx = (ArrayList<yourClassImplementsParceable>) intent.getParcelableArrayListExtra("tag");

hope this helps




回答2:


Try retrieving the array with

getIntent().getExtras().getParcelableArrayList(yourArray);



回答3:


Instead of writing the Parcelable implementation yourself, try to use the Android Studio code completion (in Windows: Alt+Enter once you write "implements Parcelable" and again on the CustomObject class name). This prevent common bugs in the implementation.

Even so, beware: if you implement Parcelable and add/remove object variables in the CustomObject afterwards, the Parcelable implementation won't be valid any more.

In this case, you can manually add the missing details to the Parcelable methods, but I prefer to delete them and make Android Studio implement them again (be sure to delete ALL of the implemented Parcelable methods in the CustomObject, since Android Studio won't update Parcelable methods that already exist).

I suffered the same bug that you wrote about, and, after correctly re-implementing Parcelable on the CustomObject (I had changed it a few times since I last implemented Parcelable), everything worked fine.



来源:https://stackoverflow.com/questions/5736396/passing-arraylist-between-intents-loses-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!