Passing ArrayList with objects to new Activity? - android

I'm trying to pass an ArrayList from my first Activity to the next one. Basically, the first activity parses an XML file and creates an ArrayList with objects inside. What I want to do is send that ArrayList to my second activity and show some of the object data in a ListView.
I thought of doing this with an intent, but it looks like only primitive datatypes are usually passed through intents. Is this right?
If so, what would be a better solution to pass the data? Certainly Android must provide something to be able to do this kind of thing.
Any help/code examples are really appreciated..
Thanks
EDIT:
I solved this by first creating and calling the intent, and only parsing the XML in the Activity that I called. This way I didn't need to pass the objects anymore. But for those interested, you can read about how to pass data through activities, here.

Complex types may be passed by means of Parcelable. An example is in this question:
Help with passing ArrayList and parcelable Activity

I would do a toString on the array and pass it as an extra by doing a intent.putExtra("label". array.toString()); and then just recover it in the new activity.

You can pass a String List using putStringArrayListExtra(String name, ArrayList<String> value) if it is strings. Or, you could serialize List and then use putExtra(String name, Serializable value).
If you don't want to/can't use the above, you could use a central util class with a static reference to the List. Just set it in your first Activity and get it in the second.

This could be totally bad practice and I wouldn't know any better, but you could declare the ArrayList as public and static. Then just access it with Activity.ArraylistName.

You can set the scope of ArrayList at application level or you can do this using parcelable.

I made this trick to send from first Activity to second.
The first activity
ArrayList<String> mylist = new ArrayList<String>();
Intent intent = new Intent(ActivityName.this, Second.class);
intent.putStringArrayListExtra("key", mylist);
startActivity(intent);
The second activity
To retrieve
ArrayList<String> list = getIntent().getStringArrayListExtra("key");

Related

Transfer ListView with intent

I want to fill ("calculate") a ListView in a Loading Activity and when it's ready, start the new Activity with the ListView filled. So I don't have to wait to fill it in the second activity. I already have the code to fill it, but it's in the second activity.
Thanks
First, this is not really possible. You cannot readily transfer widgets or adapters to another activity.
Second, this is not really necessary. Creating a ListView is fairly cheap. If you are experiencing performance anxiety, use Traceview to determine where the problem is, then scope your plan to deal with the problem. For example, perhaps it is your model data, not the ListView itself, that you need to load in advance. Or, perhaps you just need to move some work to background threads, such as loading images to go into the ListView rows.
Third, if these two bits of UI are this closely coupled, they should not be separate activities, but rather one activity. Whether you use fragments, or hide/show widgets, or whatever, is up to you.
Your code for Activities, User Interface that may contain creating custom views , AND code for adapters must be independently defined or in separate packages if u prefer to go for clean coding having done that u will not face above problem and also u can REUSE your code .
What you really want is to transfer the data from one activity to another activity. Instead of passing the "ListView", you should be passing the data that you need to populate in the other activity
Intent intent = new Intent(CurrentActivity.this, OtherActivity.class);
intent.putExtra("SOME_KEY", mySerializableData);
startActivity(intent)
You can pass any type of serializable data using intents.
So you should ideally:
Create your adapter somewhere outside of your CurrentActivity and
OtherActivity
Pass the mySerializableData from CurrentActivity to OtherActivity
Use the same adapter to populate the ListView using the data passed in the intent using getIntent().getSerializableExtra()
Use instanceof to ensure that what getExtras() returned is of the type MySerializableData
So for example if we want to pass a ArrayList<MySerializableData>
Object obj = getIntent().getSerializableExtra("SOME_KEY");
if (obj instanceof List) {
//Do stuff
}

Sendind an Extra with my own kind of data

I need to pass an extra data to an activity, but I don't know how since my data is a variable from a specific class of mine. I have something like:
MyClass variable=new MyClass();
Intent intent= new Intent(this,SecondActivity.class);
intent.putExtra("variableName", variable);
I know the error in code is obvious, but I want to know how can I send data from a class of mine.
Please looking into Parcelable
How can I make my custom objects Parcelable?
http://developer.android.com/reference/android/os/Parcelable.html
You will have to serialize the data and then pass. Android doesn't support passing custom objects as extras. Look at this answer here. Passing custom objects between activities?

How to transfer an ArrayList of objects from one Activity to another

I have an ArrayList of objects in one activity and I need this arrayList in another activity.
Is there a solution to transger this ArrayList? I know i can use intents for ArrayList of Strings but what's about the ArrayList of objects?
Thank you.
Complex types passed by means of Parcelable or do the serialization to another kind of primitive object that you can put to Intent's extras. see this question:
Help with passing ArrayList and parcelable Activity
and this tutorial:
Passing a list of objects between Activities

Pass complex object though intent

I need to pass a list of cookies from one activity to the other using an Intent. How would one go about achieving this?
Thanks.
Using Intent.putExtra(String name, String[] value) would be the easiest way to pass a list of cookies. There are other putExtra signatures as well, depending on how you currently have your list implemented. On the other side, you would use getExtras() to get values. If for some reason, you had a more complex setup, you could create a Cookie class which extends Parcelable.
Just pass it along in the setExtra() method of the Intent class.
Hope it helped,
JQCorreia

Passing arraylist in intent from one activity to another

I have a class 'Product' and i need to pass the arraylist of 'Product' objects from one activity to another.
I read that i can do so by making my Product class as Parcelable and use : putParcelableArrayListExtra (String name, ArrayList value).
In my app, i need to pass arraylists of objects from one activity to another. In this case, i need to make all those classes as parcelable and implement the interface methods.
Is this the correct way to solve the problem? Is there any other way to send the list of objects in intents?
Thanks..
Yes, the correct way is to use Parcelable or do the serialization to another kind of primitive object that you can put to Intent's extras

Categories

Resources