how to read a java object from android resources - android

I am still new to android and there is probably an obvious answer to my problem. But I have not found the correct question to ask or google for.
What I want to do is read in a xml representation of a (list of) java object(s) and have the objects accessible from my activity.
Is this possible with android resources?
After some reading through the internets the best solution I found was to write the objects as json string and save them in res/values.
And then use something like Gson to parse them as java objects.
But there has to be a better way.
What would be the standard way to approach this?
To add some context: I am programming a small android game and would like to have a research-tree.
Each research item has properties like a name, costs, effects and of course a list of references to other research items as preconditions.
I would appreciate any ideas and suggestions.

If you don't want to use 3rd party solutions, the recommended way is to use the platform's XmlPullParser:
https://developer.android.com/training/basics/network-ops/xml.html

Related

Is It possible to read serialization data in android which made by iphone development

I have one file which is create in iPhone using serialization object. and i want to do read in android. Is it possible or not? please reply fast in advance thanks
I am assuming that the serialized data is in a pList.
See this http://code.google.com/p/plist/
You should be able to read any file generated by another language. It's better if there's a library out there for it. It'll get complicated if the file uses arrays, especially dynamic ones. This may require some fun time with a hex editor. I can't guarantee reliability with this method though if the data isn't simple. Just realize that this may end up being a tricky task
It's better to have a file spec made. You don't have to use a binary file, as you can use something like JSON or XML if it's all text-based information.

Best Way to Store Custom Objects to Internal Storage in Android?

I have a custom object, "TimeSheet", which itself contains Calendar, DateFormat, and int fields. The App is designed to use several of these objects, so I'm planning on storing them in a List as they're created and I'd like the App to be able to save these objects to internal storage when the App closes and reload them when it opens.
I'm still something of a novice when it comes to Android development (I've only published one App so far), so I'm not entirely sure of the best way to go about this. I'm guessing an ObjectInputStream and its Output counterpart are probably the best options, but I'm not entirely sure. I'm completely willing to change my design strategy to store a collection of these TimeSheet objects in the easiest way possible.
Can anyone recommend a good direction to go from here, and if possible, provide brief, simple examples?
Thanks!
There is no single right answer for something like this. A lot of it depends on the amount of data that you are storing. If you don't have much data, used SharedPreferences, if you have lots of data and it is complex, use a database. I wouldn't use a database if you don't have much data. You want to keep things as simple as possible and adding a database can complicate things. Here is a link that talks about the different options. Check it out. Hope it helps:
http://developer.android.com/guide/topics/data/data-storage.html
There are 2 ways to do this
Save it in a SQLite database..
Save the objects in a json format in a file
See this discussion
I'd honestly recommend using a SQLiteDatabase to store them: write functions to map your 3 fields to the database (Calendar would become a NUMERIC, DateFormat would be a String, and the int fields would all be NUMERICs) and to rebuild your object fields from a row in the database. Its a bit heavy up front but will make the inevitable feature expansion much easier.

Easiest way to store a list a my own class in a android app?

What is the best/easiest way to store a list (LinkedList) of a class which I created in an Android application?
In apps in other plattform it could be done using:
a ORM framework
the database
serilization and store the file
But my question is: Which of those 3 is the easiest to implement it in an Android application without using a lot of time to make this code?
Using directly SQLite in my opinion is the easiest and most straightforward way to do it. I would consider an ORM-Framework only if I have a lot of classes to store. But I am assuming that's not your case.
Straight serialization to a file might be a good idea, but I would prefer SQLite to have the possibility of using database versions, adding indexes and performing more complex search on the data easily.
It depends on how you inted to use this data. Simplest way is inherit you class from Serializable and dump LinkedList to a file. This works great if you don't need random access to elements of this list.
SQLite is good and flexible but it requires much more coding effort.

Does Android's JSON parser support revivers?

I was trying to usefully answer this question, and figured that the questioner would want to do the conversion of the .Net-style JSON dates ("/Date(######)\/") to dates via a reviver, but looking at the documentation it's not clear to me that Android supports the concept of a "reviver". Does it?
The answer would appear to be: No, not directly. But yes, you can do it (or a form of it) without too much work:
No, in that JSONObject, JSONArray, JSONTokener, JSONReader, etc., don't seem to support the concept directly.
Yes, in that you could probably support a simple form of reviving (such as a context-free substitution of strings in a given format with corresponding Date objects) with little effort via JSONReader, or even (with just a little more work) create a fully-fledged reviver-enabled deserializer (complete with getting the order of things right, etc.). So not built-in, but not from scratch.

Simple Access w\Read&Write to XML file using Android

I'm new to android development, and I'm trying to work a very simple database solution. I have a max of five variables I want to store when the app is installed, and I want the user to be able to change these variables and their changes be remembered after they've closed the app. While I could use the built in SQLite feature, that's way more complex than I need.
Normally in other languages, like VB, C#, and Javascript, I just load the XML into an XML object and then navigate the nodes. Is there any way to do that in android? This solution should only be a dozen or so lines of code within a single class.
If this isn't possible, what is the simplest way to store and retrieve simple data?
This is a common question and of course there is a way. Use Simple XML. I wrote a blog post on how to make that happen.
Essentially the Simple XML library lets you marshal and un-marshal XML to and from Java Classes by correctly annotating them with Simple XML Annotations. Just read the simple tutorial and my blog post to understand what I mean.

Categories

Resources