how to serialize ArrayList on android - android

i have a basic function which requires serializing in my android app. The user will add some values to an ArrayList and i want to serialize it to avoid using a database for this little option and of course TO LEARN how to serialize (i'm a begginer) because it seems useful. Anyways the user save something in the arraylist, the program shuts down, the program starts up again and the user is able to see the saved data. How can i implement this? Can you provide some code snippet or a useful link?
Thanks a lot!!

You can do this by custom bean class and implement Serializable to that
so now when you create ArrayList<E> of that class it is Serializable.
Example:
Class dataBean implements Serializable
{
public String name;
}
ArrayList<dataBean> dataBeanArrayList = new ArrayList();
So dataBeanArrayList is now Serializable and you can also pass this between Intent.

I suggest using flexjson to serialize the data to a file. Then you can read that back using that library. This has several advantages over serialization which is being able to load your stream back into potential differing versions of your objects. Using ObjectInputStream you have to be very careful, and quite frankly I've never seen it work all that well.
http://flexjson.sourceforge.net
Here is a blog post how to do that:
http://wrongnotes.blogspot.com/2010/09/flexjson-meet-android.html

Related

Passing objects in Android that I didn't create?

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.

Is passing with serializable or parceable an efficient solution?

Im building an android app and in the startup activity i parse a pretty big json file (3.3 mb) into custom objects, or when there was no update i retrieve it from a serialized bytearray. Im dealing with one object with a list of about 500 objects with subobjects, lists etc.
Now i need this data, or some of it in my other activities. What is the best solution for this?
It seemed a lot of data processing to serialize and deserialize using intent.putExtra or using parceable everytime you start a new activity. Is this processing less than i think or is there a way to use your parsing class and don't destroy it so you can use something like
Myclass.get(nrIneed).Mysubclass.getsomestring
?
This is how i did it when using data for logging or something in my parsing activity.
You can use Application class to store this data and you can use it across all the Activity
public class BusinessClass extends Application
{
public ParsedData parsedData = new ParsedData();
}
Then call it in any activity using following code
BusinessClass appState = ((BusinessClass)getApplicationContext());
appState.parsedData.getData();
For more info
http://developer.android.com/reference/android/app/Application.html
You SHOULD NOT use Parcelable for objects which may consume memory more than about 1MB. Otherwise the parsing will fail (at least as per API level 8).
However, in your case, I would recommend you to save/organize the parsed data into SQLite and query it from other activities. This will help your app to eat less memory :)
You may also create a static reference to your object, but since its huge in size, I wouldn't recommend you, because then your app will become an appealing target for android VM to kill - when running under low memory circumstances.
I think you shouldn't use your data as a big Json file. At the first launch you should save your data in a database then only use this db when you need to Create/Retrieve/Update/Delete.
If you really want to use your JSON file, then you should make it static (in your application singleton for example).
Hope this will help you

How to access natively allocated data across threads on Android

Question:
Is it possible to share natively allocated data across multiple threads on Android?
Example:
In the onCreate() function, I allocate a struct on the heap using native code and return a pointer to that data.
Later on in the application, I would like to use that data in a different thread, in my case a GLThread used to render the data out...
Is this possible? If so, what would be the best way to go about this?
I dont know if I understand the problem, you want to have some kinda share data object?
In my case I use something like this:
Create new class, which extends from application and there you can keep the variable with public getter and setter.
In any of your intent you can just call for this class:
DataHolder data = (DataHolder) getApplication();
Don't foget to add this class to manifet as application.

Save (store) list of simple objects on Android

I need to save List of simple objects
class simple{
String name;
byte[] data;
}
in android. How to do this ? Do I need SQLite or there is easier way ?
Just refer serialization in java, and that makes your objects to be serialized.
see an example here,
http://www.jondev.net/articles/Android_Serialization_Example_%28Java%29
I'd use a database for that. It would make easier to get that data from different activities. Here is a great tutorial on dealing with database in Android.
I assume you can take that byte array as an INTEGER one for database type compatibility (or maybe some clever conversion to something else). The tutorial above provides you with info on the data types.

Best way to make nested custom objects parcelable in Android

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.

Categories

Resources