1

I posted this question yesterday in the Java forum, but it's probably more appropriate in the Android section:

Java object added to ArrayList not showing

So I passed an ArrayList via a bundle however whenever I took a subject's Task ArrayList and added something the changes did not appear in the ArrayList.

To fix this instead of casting the Subject I passed the index in the ArrayList and did the subject assignment from the index.

So the Subject assignment was subject = subjectsList.get(index); instead of:

Bundle bundle = getIntent().getExtras();
  if (bundle != null) {
    subject = (Subject) bundle.get("selected_subject");
    subjectsList = (ArrayList<Subject>) bundle.get("subjects_list");
   }

I'm still casting the ArrayList so I'm not sure why it does n't work when casting the subject. Can anyone explain this please. I think it may be do with the subject not being the same as the one in the ArrayList but I'm not sure.

Also surely the subject and ArrayList created in one activity and passed to another via a bundle are referring to the same object?

Community
  • 1
  • 1
user1424720
  • 61
  • 1
  • 10
  • Are you checking the logcat? It might be that the bundle.get("list_name") call returns something which cannot be cast to an ArrayList – cnexus Feb 02 '13 at 15:46
  • There's no logcat error message. Everything appears to work fine but when I iterate over the list the new value does not appear. If I iterate over the subject's list the value is there, hence I think it is not referring to a Subject which is in the ArrayList – user1424720 Feb 02 '13 at 15:52
  • 1
    I don't think that they're referring to the same object anymore. Once you `Bundle`, the values are Serialized. This means the object is broken down then reconstructed in the other Activity. If you work on the ArrayList in Activity2 then go back to Activity1, the changes should not be visible IMO. – A--C Feb 02 '13 at 15:53
  • Yes I think that is what's happening. Then what is the point in allowing objects and ArrayList's to be passed to other activities? Also what are my alternatives for accessing the list from another activity? – user1424720 Feb 02 '13 at 15:57
  • @user1424720 You can constantly pass `Intents` with bundles back and forth, but It seems wasteful. Having some sort of database would probably be nicer, as it's centralized storage, accesible by any Activity. – A--C Feb 02 '13 at 16:02
  • Ok, thanks. A database seemed like too much work for what is a toy app for a project. Every other comment I've seen says that if you implement Serializable it should work. http://stackoverflow.com/a/6425138/1424720 I may try to make the lists a public static variable even though this is not always good practice. – user1424720 Feb 02 '13 at 16:12

0 Answers0