Daniel
Daniel

Reputation: 3374

Dictionary in android resources

Is there a way to use dictionary in android resources. I couldn't find anything in String.xml file about dictionary and searchable file has many other fields that I don't need.

Edited: in C# and visual studio we can add dictionary to the resources as well as string or int, however, I couldn't find any option for adding dictionary to the resources and only string and string array or integer are available on android resources.

Upvotes: 2

Views: 7242

Answers (2)

Dmitry Babienko
Dmitry Babienko

Reputation: 131

maybe it`s late to answer here but you can use something like this:

res/strings.xml

<string-array name="tags">
   <item>KEY_A:Some string value 1</item>
   <item>KEY_B:Some string value 2</item>
   <item>KEY_C:Some string value 3</item>
</string-array>

Here, the ":" symbol stands for delimiter between key - value pairs in string array.

And then somewhere in code(e.g. onCreate()):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String[] tags = getResources().getStringArray(R.array.tags);
    for(String tag : tags) {
        String[] pair = tag.split(":");

        String key = pair[0];
        String value = pair[1];

        // do whatever you want with key and value
    }
}

This approach is good if you have simple data for more complex data better to use res/raw files with json objects, as described by other guys above.

Upvotes: 12

drakyoko
drakyoko

Reputation: 505

There is no such a resource type. You have to put an arbitrary file in res/raw/ and import it manually in a HashMap.

Upvotes: 3

Related Questions