Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass arraylist of user defined objects to Intent android

I am trying to pass a structure of arraylist to an intent as follows, In the calling function i am using

ArrayList<Parliament> s=(ArrayList<Parliament>)msg.obj;                                 
Intent i = new Intent(ReadTasks.this, GenrateTasks.class);                          
i.putExtra("tasks", s);
startActivity(i);

And in the called function

Bundle b = getIntent().getExtras();
if(b!=null)
{           
    ArrayList<Parliament> as = (ArrayList<Parliament>)b.getSerializable("tasklist");
}

I have given implements serializable in both the classes, but am getting unable to marshall.. run time error in the calling function at line start activity. Please help how to proceed with!

Edit 1: Stack trace:

04-01 22:21:53.999: E/AndroidRuntime(2078): FATAL EXCEPTION: main
04-01 22:21:53.999: E/AndroidRuntime(2078): java.lang.RuntimeException: Parcel: unable to marshal value com.viralm.readjson.Parliament@413597f0
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.os.Parcel.writeValue(Parcel.java:1137)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.os.Parcel.writeList(Parcel.java:524)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.os.Parcel.writeValue(Parcel.java:1097)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.os.Parcel.writeMapInternal(Parcel.java:493)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.os.Bundle.writeToParcel(Bundle.java:1612)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.os.Parcel.writeBundle(Parcel.java:507)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.content.Intent.writeToParcel(Intent.java:5846)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1606)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.app.Activity.startActivityForResult(Activity.java:3190)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.app.Activity.startActivity(Activity.java:3297)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at com.viralm.readjson.ReadTasks$1.handleMessage(ReadTasks.java:61)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.os.Looper.loop(Looper.java:137)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at android.app.ActivityThread.main(ActivityThread.java:4340)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at java.lang.reflect.Method.invokeNative(Native Method)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at java.lang.reflect.Method.invoke(Method.java:511)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-01 22:21:53.999: E/AndroidRuntime(2078):     at dalvik.system.NativeStart.main(Native Method)

My Parliment class is having 5 strings with its setters and getters!

like image 359
bharath Avatar asked Apr 01 '13 16:04

bharath


3 Answers

I have given implements serializable in both the classes, but am getting unable to marshall.. run time error in the calling function at line start activity. Please help how to proceed with!

Since you are using Serializable, you cannot do it like now. I suggest you to wrap your ArrayList to class for example named DataWrapper. This class also needs to implement Serializable and then you are able to pass ArrayList via Intent.

Example:

public class DataWrapper implements Serializable {

   private ArrayList<Parliament> parliaments;

   public DataWrapper(ArrayList<Parliament> data) {
      this.parliaments = data;
   }

   public ArrayList<Parliament> getParliaments() {
      return this.parliaments;
   }

}

And an usage:

Intent i = new Intent(...);
i.putExtra("data", new DataWrapper(yourArrayList));

and retrieving:

DataWrapper dw = (DataWrapper) getIntent().getSerializableExtra("data");
ArrayList<Parliament> list = dw.getParliaments();

Note:

There is also option to use Parcelable interface. If you will use it, you can put and retrieve your ArrayList with these methods:

intent.putParcelableArrayListExtra("key", ArrayList<T extends Parcelable> list);
getIntent().getParcelableArrayListExtra("key");


Generally is recommended to use Parcelable interface that is directly designated for passing objects through Activities but i usually use Serializable interface and it always make a trick.

Also be carefull about typos. You are putting object with key task and you should retrieve it with same key and not with tasklist.

like image 111
Simon Dorociak Avatar answered Nov 10 '22 00:11

Simon Dorociak


Implementation of Serializable is deemed low performant where as Parcelable is encouraged.

I had the exact same question and while still hassling with the Parcelable, I found out that the static variables are not such a bad idea for the task.

You can simply create a

public static ArrayList<Parliament> myObjects = .. 

and use it from elsewhere via MyRefActivity.myObjects

I was not sure about what public static variables imply in the context of an application with activities. If you also have doubts about either this or on performance aspects of this approach, refer to:

  • What's the best way to share data between activities?
  • Using static variables in Android

Cheers.

like image 34
Orkun Ozen Avatar answered Nov 10 '22 01:11

Orkun Ozen


Make your object class Parcelable later try to use

bundlename.putParcelableArrayList
like image 28
Soniya Mohan Avatar answered Nov 09 '22 23:11

Soniya Mohan