1

string.xml contains lot of keys with value. I want to get particular value based on key.

For example, string.xml contains key1, key2 up to key100. I displaying these keys into ListView. If user select the key50 means I want to displaying the corresponding value in TextView. How to achieve this?

Sample string.xml file,

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
    <string name="key1">value 1</string>
    <string name="key2"> value 2 </string>
     ...
     ...
    <string name="key100"> value 100 </string>
</resources>
2
  • Can you show your String.xml file?? Commented Oct 18, 2011 at 11:57
  • 1
    What exactly is in your ListView? Text strings such as "key1", "key2" etc. etc.? In that case you can get the value of the string resource by name - first get the id by calling the "getIdentifier" method in the "android.content.res.Resources" class (defType=string, defPackage=your package name). Commented Oct 18, 2011 at 12:12

3 Answers 3

4

You should save your key as a string array resource like this

<string-array name="my_keys">
    <item>Key 1</item>
    <item>Key 2</item>
    <item>Key 3</item>
    <item>Key 4</item>
    <item>Key 5</item>
    <item>Key 6</item>
</string-array>

Populate the listView with the following code.

String[] myKeys = getResources().getStringArray(R.array.my_keys);
ListView mListView = (ListView)findViewById(R.id.yourListView);
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myKeys));  

Retrieve the clicked item and put the value into the TextView

TextView myTextView = (TextView)findViewById(R.id.yourTextView);
  mListView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      myTextView = ((TextView) view).getText();
    }
  });
Sign up to request clarification or add additional context in comments.

2 Comments

This should be get the selected value in the ListView. But I want to get value from string.xml based this selected value.
The string.xml contains the string-array and other resources you define there. Its up to you to retrieve those variable based on the clicked item. Anytime you wish to read the value of a resource, just use getResources()
3

If you have a <string name="key">string value</string> resources then the only way is to access them by key:

String value = context.getString(R.string.key);

Btw, if you need to store some arbitrary data into your Views (the ones inside your ListView), use view.setTag(object) and view.getTag().

2 Comments

yeah. I know. But I need to get particular key value based on user selection.
I updated the answer. Basically you can store/retrieve arbitrary objects inside the View.
0

I will suggest to use XPath to get your value for keys.

1 Comment

Why dont you simply use the context.getResources.getString() ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.