-1

I'm wondering if it is somehow possible in Java (for Android in this specific case) to get from an ArrayList of strings a set of static, preferably final, variables stored in a resource class or enum, like aapt does for the resource class "R".

An example, if I have the following ArrayList:

ArrayList<String> AL=new ArrayList<String>();
AL.add("chat");
AL.add("greeting");
AL.add("conversation");

method_to_get_the_class(AL); //or something like this......

I'd like to get somewhere a static class like

static class ModeList {
   public final static String CHAT="chat";
   public final static String GREETING="greeting";
   public final static String CONVERSATION="conversation";
}

Do you think it could be possible, maybe using some kind of build pre-processing method like Android's aapt? Maybe with Gradle (which I don't know very well)? The final target is to be able to recall each ArrayList entry by a variable named like its content, exactly like layout resources of Android.

Thanks

martin.p
  • 207
  • 3
  • 13
  • Enum? HashMap with key named as the value it contains? Why using arraylist in the first place? – solar Aug 23 '15 at 15:33
  • 1
    What exactly are you trying to achieve? If you use the preferred way of storing strings in `/res/values/strings.xml` (or similar) then you're effectively providing static string resources at build time. Not only that but you can make it support multiple languages and Android will adjust accordingly at runtime depending on the users' locale. – Squonk Aug 23 '15 at 15:41
  • @Squonk What I'm trying to achieve is something like how the R class works: if I declare a layout element in XML and I give it a name (say a TextView named tx1) then I can get an Integer id through the Activity method "findViewById(R.id.tx1)". The integer id of tx1 is a final variable set in the static final class R, generated by aapt. What I need is to set a static final variable named like the content of the variable. It's a little bit different from the example above with the R class but in the end is the same concept more or less. Hope this clarifies. – martin.p Aug 23 '15 at 16:00
  • @martin.p : No, sorry that hasn't clarified things. Strings declared in `strings.xml` are pre-compiled into R.java also and become `static final` resource ids accessible using `getResources().getString(int resId)`. If you have a string with the key "CHAT" and value "chat" you'd simply use `R.string.CHAT` as the resource id to return "chat" as the string. As I said, this also makes multi-language support posible by having different /res/values-XX folders where XX is a country code and Android does it automatically for you. – Squonk Aug 23 '15 at 17:11
  • @Squonk what I'm trying to achieve is outside the aapt and the R resource class, I'd like to create similar mechanism to define static final variables from xml files differenti from layout, string.xml or similar. – martin.p Aug 23 '15 at 20:15

1 Answers1

1
public static void CreateClass() throws IOException {
    ArrayList<String> al = new ArrayList<>();
    al.add("sun");
    al.add("mon");
    al.add("tue");

    File file = new File("RR.java");
    PrintWriter pw = new PrintWriter(file);
    pw.println("public class RR {");

    for (String temp : al) {
        pw.println("    public static final String " + temp.toUpperCase() +
                " = \"" + temp  + "\";");
    }        
    pw.println("}");
    pw.close();
}
Kashyap Kansara
  • 395
  • 3
  • 10
  • @Kansara Thanks for the answer, I will probably try tomorrow. What is not clear to me is how the class generated is included in the building path and included in the building process? – martin.p Aug 23 '15 at 20:19
  • @martin.p Above code snippet will ONLY create a java class. You have to include it manually in your project. Add path before file name if you cant find it. – Kashyap Kansara Aug 23 '15 at 21:00