Can you create an XML object resource with key-value pairs (basically a hashmap) using an XML file analogous to arrays.xml? This would be an analogue of NSDictionary from Obj-C.
i.e.
<key>foo</key>
<value>bar</value>
I'd like for the value to be an array of strings, and I'd like to be able to copy and paste from XML files in my Obj-C projects.
Thanks.
A good alternative is to create JSON objects, and access these using GSON. If you already have a PList resource from an iPhone project, one way to convert to JSON is to create an object from the PList and write it to a JSON string file.
Related
I want to create a JSON file to style my google map but I get this message: E/Google Maps Android API: Map style parsing failed: org.json.JSONException: Value res of type java.lang.String cannot be converted to JSONArray
E/MapsActivityRaw: Style parsing failed. Any help?
Here is my code example where I have the method with JSON creation and on left side you see the raw package where the JSON file
Here is the error that I get
I used Google platform mapstyle.withgoogle.com. But the thing that I'm not sure about, that I created a Text Document then changed the ending to .json, copied the JSON code from google platform and put it there and then copied the file and put it in android studio. Because I couldn't know how to create a JSON file in android studio. I don't know if it sounds the right way to do that?
in the previous comment that I left you, it was because I did not have the solution, but in reviewing the google documentation well I realized that there are two types of codes to implement the styles, one by JSON files and others by means of string
For the code that you show in the image it has to have a style_gray string resource, for this case I would suppose that you would have an xml file or that variable in the string.xml, you could use that same code like this:
googleMap.setMapStyle(new MapStyleOptions(getResources().getString(R.raw.map_style_aubergine)));
For that error to be solved, you would have to implement this other code, which if it is compatible with JSON files in the format generated by https://mapstyle.withgoogle.com/, the code would be the following:
googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this,R.raw.map_style_aubergine));
In my case the map_style_aubergine is the JSON file and a variable in the string.xml you can use this as an example and make the respective corrections in your code
I want to parse a JSON file generated by my RESTful application and use values from that file in order to change some variables of the layout/style.xml like the color of the background. I really need some help with it.
If you want to convert your JSON file to java objects you can use gson (http://code.google.com/p/google-gson/). On the other hand the layout/style.xml file cannot be modified in run-time. If you want to change the background color or any other UI property in run-time you have to do it programatically. For example you can define all the necessary styles in your layout/style.xml and when you converted your JSON file into java objects with gson, you can apply the corresponding style depending on the JSON response.
I have an XML file which I have retrieved the data via a Document variable. I need to extract data for a class which contains several fields. What is the easiest way to retrieve the records from the Document variable ?
For Document objet use DOM parser.
For XML files use SAX parser.
Try to use a SAX parser for XML parsing, because it is more memory efficient.
I have two resources file in res/values directory: string.xml and names.xml
how can I retreive all resources from names.xml only
the method
Field[] x=R.string.class.getFields();
retrieves resources from both files.
how can this be acheived
thanks
I am sorry, but that is not possible. It does not matter that you have your strings split between multiple named resource files -- Android combines them all when it compiles your project.
You are welcome to use prefixes or something to identify one set of strings from another. I do that with the support code for the Android Parcel Project, to allow reusable components to each define strings without one overwriting the strings of another.
I want to create a java.util.map in android from a resource. I want to do this because I have a lot of entries to populate into the java.util.map and I want to store the values in the res folder of the project in xml format.
Is there an effecient way to do this in android? My map will have around 2500 entries so I want to do this as effeciently as possible and I don't want to hard code them...
Thanks,
Gaz
I think the Xml format is "too much" to simply store key-value pairs. A text file where a line is a key-value pair is more adequat (e.g. with comma separator), also the parsing will be easier.
foo1,bar1
foo2,bar2
...
Save your text file in res/raw directory
Open it via context.getResources.openRawResource(R.raw.fileName)
Loop on each lines and split the line to retrieve the key and the value.
Put them in your map
That's all