Xamarin listview with objects - android

If I have a class containing of integers, strings and a Date, how should I go about creating, saving and displaying a new instance of this class in my ListViews?
I've used SharedPreferences before for saving local strings but is this possible with objects as well, or should I look into SQLlite?
Edit: Also, if SQLlite is needed, a little steer in the right direction would be appreciated.

You can still use preferences to save your objects but you need to serialize them into strings. And for later use you have to deserialize them from strings into your list of objects. This can easily be done with Json.net but I would recommend to use a database like SQLite. This would make it easy if new requirements like searching or object extension comes up.
A good entry into SQLite and Xamarin can be found here or here.

Related

Android - Save preferences per object

Is there any way to save preferences per complex objects?
I have a dynamic list of objects that each contains 3 strings and 2 dynamic lists of pair of booleans. Is there any way to save those objects like preferences?
You have the option to serialize your complex objects to a file. Then deserialize to recreate the object back from the file. This is not much different from saving to SharedPreferences.
You can refer the code here. https://stackoverflow.com/a/5816861/2107118
Note: Do make your complex object implement Serializable interface and provide a serialVersionUID.

How to serialize/deserialize object in android with LinkedList of other custom objects inside?

How to serialize/deserialize object in android with list of other objects inside ? I done this with sqlite to manually save all fields and in another table objects from list but I am curious is there any easier method.
A fairly easy way to serialize/deserialize an object would be to use JSON and a wrapper like Google Gson that will take care of all the hard work for you.
You haven't mentioned the purpose of serializing/deserializing these nested objects, but Gson will give you a nice little (json parseable) text file to do with as you please.

Persistent storing of ArrayList

I have following ArrayList in Android:
private ArrayList<Integer> Array= new ArrayList<Integer>();
It will grow over time via add()
It will approximately contain up to 50 elements.
I want to store it persistent. I was thinking of xml, sharedpreferences and DB, but I am not sure what is the best way to go.
Saving to SharedPrefs is probably the quickest.
Here is a question that shows how to marshal an array of strings into json and then store in SharedPreferences (and read them back again).
Just change the String array to an Integer array and you're done.
How can write code to make sharedpreferences for array in android?
I would use whatever else you are using in your app - ie keep similar things in similar places.
One benefit of using sharedpreferences is that they can be backed up (if you implement that service)... However using a DB may give you more flexibility in the future.
That doesn't really answer the question though. From how you describe it, it does smell like preferences would be better though.

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.

How to save parsed data in android?

I am creating an android application in which I am parsing so much XML data (XML data comes from a server) which contains Strings and image url's also.I need to use this data in many part of application. So for saving all these data I have used ArrayList and HashMap.I have declared ArrayList's and HashMap's variables in a single class as public static variables so I can access this data through a single class whenever I need.
And for images I have created ArrayList and placed in same single class same as other data(public static).Once I download a image through image url's I save those image drawables to these ArrayList variables so whenever I need any image again so I use these variables to get it.
Now my doubt is whether this approach is right or not. Please suggest me the right way.
Thank you
You should actually take a look at: http://developer.android.com/guide/topics/data/data-storage.html
It all depends on how you use the data, does it need to be updated all the time when the user opens the app you could take a look at this answer: How to declare global variables in Android? where you declare a global application and have the variables there.
Otherwise I would actually advice to use an sqlite database. http://developer.android.com/guide/topics/data/data-storage.html#db One good example is here: http://www.vogella.de/articles/AndroidSQLite/article.html

Categories

Resources