[android-developers] Re: Generating a static HashMap from resources?

2010-04-15 Thread Bob Kerns
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

[android-developers] Re: Generating a static HashMap from resources?

2010-04-15 Thread HippoMan
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

[android-developers] Re: Generating a static HashMap from resources?

2010-04-15 Thread HippoMan
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

[android-developers] Re: Generating a static HashMap from resources?

2010-04-13 Thread Walter
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 hippo.mail...@gmail.com wrote: My main reason is that I want to have public static final mapped values available to

[android-developers] Re: Generating a static HashMap from resources?

2010-04-13 Thread HippoMan
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

[android-developers] Re: Generating a static HashMap from resources?

2010-04-12 Thread HippoMan
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

[android-developers] Re: Generating a static HashMap from resources?

2010-04-11 Thread HippoMan
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] Re: Generating a static HashMap from resources?

2010-04-11 Thread Streets Of Boston
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:

[android-developers] Re: Generating a static HashMap from resources?

2010-04-11 Thread Bob Kerns
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