0

I am trying to pass arraylist from one activity to other using Bundles in OnPostExecute method,i am not able to do so.I am not getting proper error also to typecast or do stuffs to remove error.I am not sure what is wrong here.

   ***here reminderList is List<GetReminder> reminderList;***

private class AsyncCallWS extends AsyncTask<String, Void, Void> {
            @Override
            protected Void doInBackground(String... params) {
                //Invoke webservice
                vaildUserId=WebService.invokeAuthenticateUserWS(loginUserName, loginPassword, "AuthenticateUser");
                if(vaildUserId>=0){
                    reminderList=WebService.invokeHelloWorldWS("GetReminder");
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("reminderList", reminderList);
                reminderIntent.putExtras(bundle);
                startActivity(new Intent(getApplicationContext(), ReminderActivity.class));
                startActivity(reminderIntent);
            }
jenil
  • 141
  • 11

2 Answers2

1

The problem is your List<GetReminder> reminderList; is not a String List.

To pass Custom Object List you have to make your Custom class either Serializable or Parcelable. In your case make GetReminder as Serializable or Parcelable.

Then use putExtra() or putSerializable() of Intent to pass Serializable object.

Also some supercilious code I noted from your onPostExecute() as you have written

startActivity(new Intent(getApplicationContext(), ReminderActivity.class));
startActivity(reminderIntent);

will cause creating two activity instance.

so remove first one,

startActivity(new Intent(getApplicationContext(), ReminderActivity.class));

user370305
  • 108,599
  • 23
  • 164
  • 151
  • For reference http://stackoverflow.com/questions/15747727/pass-arraylist-of-user-defined-objects-to-intent-android and http://developer.android.com/reference/android/os/Parcelable.html – user370305 Mar 20 '15 at 15:19
  • :) yes i added by mistake thanks for noting. let me try out using parceable – jenil Mar 20 '15 at 15:20
1

To solve your problem, you can use putParcelableArrayListExtra() and getParcelableArrayListExtra() methods which are defined in the Intent class.

1.Make sure your GetReminder class implements Parcelable .

Here is the document of Parcelable and it also contains a typical implementation of Parcelable.

Here is a website which can help you generate an Parcelable implementation of a class automotically.

2.In your onPostExecute() method put extra like this:

//Remember to declare reminderList as ArrayList here.
ArrayList<GetReminder> reminderList = ...;

Intent intent = new Intent(getApplicationContext(), ReminderActivity.class);
intent.putParcelableArrayListExtra("reminderList", reminderList);
startActivity(intent);

Then in your ReminderActivity class get the ArrayList like this:

ArrayList<GetReminder> list = getIntent().getParcelableArrayListExtra("reminderList");

BTW, there is also another way to solve your problem, you can refer my answer here.

Community
  • 1
  • 1
Lei Guo
  • 2,550
  • 1
  • 17
  • 22
  • getting putParcelableArrayListExtra cannot be applied to intent,one question just implementing parcealbe in GetReminder is enough? – jenil Mar 20 '15 at 15:25
  • @jenil Make sure the type of reminderList you passed to putParcelableArrayListExtra() is declared as ArrayList. And yes, just implementing parcealbe in GetReminder is enough. I'll change the code to show how to solve the problem you just met. – Lei Guo Mar 20 '15 at 15:28
  • java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sampleapp/com.example.sampleapp.ReminderActivity}: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.example.sampleapp.GetReminder – jenil Mar 20 '15 at 17:01
  • I changed has you said now i am getting this – jenil Mar 20 '15 at 17:02
  • finally worked i followed has u posted http://stackoverflow.com/questions/28733439/parceable-arraylist-of-objects/28733592#28733592 using serializable – jenil Mar 20 '15 at 17:29
  • @jenil You get that Exception because you didn't define GetReminder class as Parcelable correctly. To define a class as Parcelable, you need to add a CREATOR to it as well as implements those abstract methods. I update my answer so you can see how to do that at step 1. – Lei Guo Mar 21 '15 at 09:09
  • yes i got it,but for now i am using your second approach.Can you please see this http://stackoverflow.com/questions/29084207/parsing-webservice-anytype-response – jenil Mar 21 '15 at 09:41