[android-developers] Re: Generating a static HashMap from resources?
Yes, I forgot that ever since Java 5 (or maybe earlier?), the Java Memory Model definition specifies that classes are ininitialized "just in time"; i.e., not until they are first accessed. I mulled this over for a while, and in the end, I still opted for code generation. Although the code generator, itself, required a little bit of work, the generated class with the static definitions is straightforward and simpler to use than the nested static classes that would be necessary when utilizing "just in time" initialization. In any case, thank you very much for your thoughtful responses and your useful suggestion. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Generating a static HashMap from resources?
Yes, I forgot that ever since Java 5 (or maybe earlier?), the Java Memory Model definition specifies that classes are ininitialized "just in time"; i.e., not until they are first accessed. I mulled this over for a while, and in the end, I still opted for code generation. Although the code generator, itself, required a little bit of work, the generated class with the static definitions is straightforward and simpler to use than the nested static classes that would be necessary when utilizing "just in time" initialization. In any case, thank you very much for your thoughtful responses and your useful suggestion. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Generating a static HashMap from resources?
Not quite the only way, actually. But it does depend on loading the class that defines the fields AFTER you get your hands on the context. What you can do is to save that context in a static (non-final) field, in a different class. Then, in the class with your fields, you include a static method that computes the necessary values. This will reference the saved context in the class above to do any resource loading. Your static final fields then look like this: public static final HashMap MYTABLE = loadHashMap(R.array.mytable_keys, R.array.mytable_values); or public static final HashMap MYTABLE = loadHashMap(R.xml.mytable); That's kind of convoluted, however. You have to be careful not to touch the class too early, which may cause you to also warp other parts of your application. You'd have to decide whether that's better or worse than doing code-generation. On Apr 13, 4:13 pm, HippoMan wrote: > Thanks, but your suggestion won't work for me, because I can't > statically access the values of arrays defined as resources. These > arrays can only be retrieved via an already created Context object > through the use of getResources().getStringArray(). > > Recall that I am looking to use these values to instantiate static > final fields. > > The only way to do this is via code generation. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en To unsubscribe, reply using "remove me" as the subject.
[android-developers] Re: Generating a static HashMap from resources?
Thanks, but your suggestion won't work for me, because I can't statically access the values of arrays defined as resources. These arrays can only be retrieved via an already created Context object through the use of getResources().getStringArray(). Recall that I am looking to use these values to instantiate static final fields. The only way to do this is via code generation. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en To unsubscribe, reply using "remove me" as the subject.
[android-developers] Re: Generating a static HashMap from resources?
Why not just use two arrays and put them in your hashmap. There is no hashmap definition in resouce as I know. Sometimes, simple way is the best way. On Apr 12, 7:55 pm, HippoMan wrote: > My main reason is that I want to have public static final mapped > values available to a number of classes. This way, I can instantiate > other static final fields using some of the mappings in this HashMap. > I can't do that if I have to decode an XML file at run time. > > If I could dereference R.string.* values via a static method call, I > would be able to directly create a static class containing the > HashMap, as follows: > > public class Static { > public static final Map map = new > ConcurrentHashMap() {{ > put(X.getString(R.string.string0), > "foo-item"); > put("abc", > X.getString(R.string.string1)); > put(X.getString(R.string.string2), > X.getString(R.string.string3)); > }}; > > } > > ... where "X" is a hypothetical class that could be used to statically > retrieve the R.string.* values. But since I can only access these > strings via a method call off of an instantiated Context object, I'm > out of luck. This is another reason for why I'd like to generate a > public static final HashMap. > > Whether these reasons are considered to be "compelling" is up to the > beholder. > > Given that there appears to be no way to do this using standard > Android facilities, your XSLT suggestion seems to be a good one. > > Thank you very much. > > And yes, I did indeed mean something like @string/string0. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en To unsubscribe, reply using "remove me" as the subject.
[android-developers] Re: Generating a static HashMap from resources?
My main reason is that I want to have public static final mapped values available to a number of classes. This way, I can instantiate other static final fields using some of the mappings in this HashMap. I can't do that if I have to decode an XML file at run time. If I could dereference R.string.* values via a static method call, I would be able to directly create a static class containing the HashMap, as follows: public class Static { public static final Map map = new ConcurrentHashMap() {{ put(X.getString(R.string.string0), "foo-item"); put("abc", X.getString(R.string.string1)); put(X.getString(R.string.string2), X.getString(R.string.string3)); }}; } ... where "X" is a hypothetical class that could be used to statically retrieve the R.string.* values. But since I can only access these strings via a method call off of an instantiated Context object, I'm out of luck. This is another reason for why I'd like to generate a public static final HashMap. Whether these reasons are considered to be "compelling" is up to the beholder. Given that there appears to be no way to do this using standard Android facilities, your XSLT suggestion seems to be a good one. Thank you very much. And yes, I did indeed mean something like @string/string0. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en To unsubscribe, reply using "remove me" as the subject.
[android-developers] Re: Generating a static HashMap from resources?
Just what are you trying to achieve? If you're actually wanting a hash map, efficiently populated on class loading, then Streets of Boston's approach won't help you -- nor will aapt. To do what you actually ask for, you could write an XSLT script to generate the necessary java code. One of the things you'd have to deal with is handling the @+id/string0 stuff. Well, actually that makes no sense at all. I think you mean something like @string/string0? You'd have to make your XSLT script handle your strings.xml other values files, and map to the appropriate strings. That's probably easiest done as a separate step to consolidate them into a single temporary file. In other words, it's not all that hard to create something like this, given a compelling reason. Do you have a compelling reason? Is there a reason to not just read the XML resource and process it at runtime? On Apr 11, 11:48 am, HippoMan wrote: > In my app, I'd like to define some static resources in an xml file > which can be accessed via a HashMap. I know I can do this at run time > in a manner similar to the one which is described here: > > http://groups.google.com/group/android-developers/browse_thread/threa... > > However, since my HashMap will be a non-changing resource, I'd like to > precompile my xml into a static HashMap in a manner that is similar to > how the aapt utility precomplies the data in various xml files into > R.java. Furthermore, I'd like the keys and values to be expandable via > the "@+id/item" convention, as follows ... > > Suppose the following items exist in strings.xml: > > foo > bar > quack > oink > > Then, I would like to be able to do something like this in order to > specify my static HashMap: > > > > foo-item > @+id/string1 > @+id/string3 > > > The resulting generated code in R.java would look something like this: > > public static final class map { > public static final Map mymap = new > ConcurrentHashMap() {{ > put("foo", "foo-item"); > put("abc", "bar"); > put("quack", "oink"); > }}; > > } > > Is there a way to do anything even remotely similar to this under > Android, perhaps using aadb? > > Or am I out of luck? > > Thanks in advance. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en To unsubscribe, reply using "remove me" as the subject.
[android-developers] Re: Generating a static HashMap from resources?
Yep, Use reflection on the R.string class for strings. Or use reflection on the R.drawable class if you need to map strings to drawables. Query the (publicly) declared fields of R.string using reflection. The code below doesn't follow your example mappings exactly, but you'll get the idea: String key = "foo"; Field stringField = R.string.class.getField(mymap.get(key)); int stringFieldId = stringField.getInt(null); String stringFieldValue = getResources().getString(stringFieldId); On Apr 11, 2:48 pm, HippoMan wrote: > In my app, I'd like to define some static resources in an xml file > which can be accessed via a HashMap. I know I can do this at run time > in a manner similar to the one which is described here: > > http://groups.google.com/group/android-developers/browse_thread/threa... > > However, since my HashMap will be a non-changing resource, I'd like to > precompile my xml into a static HashMap in a manner that is similar to > how the aapt utility precomplies the data in various xml files into > R.java. Furthermore, I'd like the keys and values to be expandable via > the "@+id/item" convention, as follows ... > > Suppose the following items exist in strings.xml: > > foo > bar > quack > oink > > Then, I would like to be able to do something like this in order to > specify my static HashMap: > > > > foo-item > @+id/string1 > @+id/string3 > > > The resulting generated code in R.java would look something like this: > > public static final class map { > public static final Map mymap = new > ConcurrentHashMap() {{ > put("foo", "foo-item"); > put("abc", "bar"); > put("quack", "oink"); > }}; > > } > > Is there a way to do anything even remotely similar to this under > Android, perhaps using aadb? > > Or am I out of luck? > > Thanks in advance. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en To unsubscribe, reply using "remove me" as the subject.
[android-developers] Re: Generating a static HashMap from resources?
OOPS: I wrote "aadb", above, but I meant to type "aapt". -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en To unsubscribe, reply using "remove me" as the subject.