after parsing some information from a website using Jsoup, I would need to pass it from the main Activity to another. This information comes in an 'Elements' object.
I have read that, if I want to do that with a class I have created, I would need to implement Parcelable or Serializable. But I do not know if the Elements class from Jsoup has those characteristics.
Maybe there is a better way to do that than using Parcelable or Serializable, any suggestions?
Thanks!
But I do not know if the Elements class from Jsoup has those
characteristics.
Read JavaDoc for such information
http://jsoup.org/apidocs/org/jsoup/select/Elements.html
It is usually better to separate from 3rd part library like Jsoup and use somethink else locally. So extract important infrormation from Elements like text, links etc and pass it to your custom object for further processing. Your custom object may implement any interface.
Related
The case: I use guide from androidhive to implement two tabs that by one activity. First one - list with complex objects in recyclerview, second - map with elements from this list. As DB I use SQLite but I'm going to migrate on nice and cozy Realm.
In SQLite case general approach which looks appropriate is to make list item object Parcelable and then use bundle to transfer data from activity to fragments.
One call to the database, seems legit.
But in case of Realm I cannot use Parcelable because it requires getters/setters methods only.
What is the best way in this case?
You should only send an ID of the object you want to display and then fetch the object in the fragment as described here: https://realm.io/docs/java/latest/#intents
I belive you can make your model class implement Parcelable, it won't affect anything related to realm, you'll use the getters/setters along with the CREATOR object.
Another option is to use realm Parceler library, as mentioned in the docs, it generates the code required to make the objects parcelable so that you can pass them between activities or fragments.
I'm looking for a way of passing an object that I didn't create and cannot modify to implement parcelable in android. I was given a jar file that placed into the project by building a path to it. Now i need to pass the object created from activity to activity so that I may use the contents of the jar file. Right now it is set up so I define it as static, which probably isn't the best way. The only other option I can think of is using putSerializable but I've heard that puts strain on the system. So, what are my other options?
The main problem you have here is if that class has non-accessable private fields (through getters), then you cannot get this data to parcel it. If all private fields are accessable, then you might have several possibilities:
Extending it with a Parcelable subclass (as suggested by Simon in the comments).
Wrapping it in another Parcelable object.
Converting it to an already Parcelable object (e.g. any implementation of Map)
Note that if the object itself is not very big then the performance drop between parcelling and serializing shouldn't be noticeable. So I would go for Serializing and if the performance is not satisfactory then consider other options.
I've been just wondering if there is a way to modify variables from a different activity from which they were created. Precisely, I would like to modify a list in Activity1 from Activity 2, is there a way to give a reference to that list from the other activity? putExtra() method does not accept List as input parameter and I don't think startActivityForResponse() is what I'm looking for either. I don't know if some kind of shared variables exist or something alike.
Is it possible to do that?
Thanks in advance.
May be this is not the very good solution but what you can do is that you can create DataManager as single Instanse that Hold Linklist of data. In each activity you can get instance of data-manager and update data in it.
You can pass the entire list as an extra, you just need to serialize it by making it a parcelable first.
A Container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC
A bit confusing to understand, but all it really does is flatten the data into strings/ints/other primitive types so that it can be passed easily. On the other side, it's re-build into your list structure.
This is the tutorial I used when I did something similar:
http://prasanta-paul.blogspot.ca/2010/06/android-parcelable-example.html
There is a lot of information out there concerning Parcelable in Android. After looking around, I couldn't find a nice solution for my problem.
So, I need to create a class (let's call it "MyClass") which extends a custom class someone else wrote ("HisClass"). There are many instance variables in HisClass, some of which are objects of custom classes, themselves. I want to pass a MyClass object from one activity to another and I realize, I need to make it Parcelable. But what about the instance variables which are custom objects?
Reading this, I think I need to make every single custom object implement Parcelable. Since, there are many custom objects in MyClass and HisClass which again have custom objects for instance variables, doing that seems like a bad solution to me.
Is there a better way? Maybe, I am just being totally blind here. Hope someone can help me.
Well, you can make use of Serializer, but it would result in a slow performance. AFAIK, implementing Parcelable is the best way to passing data. Additionally, don't try to complicate yourself by creating so much complex data structure to pass using Intent.
Either use Parcelable for something cheap/light-weight/less-in-amount or something else like output to files...and passing its paths.
I want to pass a client object to a diffrent activity on android
I know how to pass strings but have no idea about passing objects.
myIntent.putExtra("nick",nick);
where nick is a string
how do i pass an object say Client c?
"If you're just passing objects around then Parcelable was designed for this. It requires a little more effort to use than using Java's native serialization, but it's way faster (and I mean way, WAY faster)."
How to send an object from one Android Activity to another using Intents?
If the activity you are passing the object to is your own, serialize the object into a string and deserialize in the activity. http://java.sun.com/developer/technicalArticles/Programming/serialization/
Or as answered here: How to pass an object from one activity to another on Android
Think hard about whether you need to send the object or if you really just need a few data elements out of the object. Sending the primitives or Strings will likely be much simpler (= faster, potentially less buggy) if it's enough to meet your needs.
If you do need to pass an object then you could either implement the Parcelable interface or you could put the object into a static variable (of type List, Set, Map, etc.) in another class and reference it that way. There are drawbacks to these approaches and I would only recommend them if you can't get by passing the actual data values you need through the bundle.
Caveat: the standard line is use simple primitives/Strings as name:value pairs, prefer parcelable over serializable
The argument is that passing serialized objects is inefficient, so do not do it. For a small object I am not so sure that this is a real world concern. It has been argued for readability and clarity over efficiency unless you detect poor performance. So far with a small object I have not detected any performance problems. IMHO, using the fully qualified name of the class in the name:value pair and serializable objects makes the code readable, simple , less buggy, easier to maintain and is easy to implement during prototyping. If performance problems are detected, or you have time to refactor the code base, then the serializable code can be converted to Parcelable code once the object properties have stabilized.
OK. I am putting on my flame suit.