Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a simple example of loading values from a values xml file into a string array, searching through the values, and selecting one for display.</p> <p><strong>values/strings.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;resources&gt; &lt;string name="hello"&gt;Hello World, Main!&lt;/string&gt; &lt;string name="app_name"&gt;Q6024120_Load_And_Search_Array&lt;/string&gt; &lt;string-array name="planets_array"&gt; &lt;item&gt;Mercury&lt;/item&gt; &lt;item&gt;Venus&lt;/item&gt; &lt;item&gt;Earth&lt;/item&gt; &lt;item&gt;Mars&lt;/item&gt; &lt;item&gt;Jupiter&lt;/item&gt; &lt;item&gt;Saturn&lt;/item&gt; &lt;item&gt;Uranus&lt;/item&gt; &lt;item&gt;Neptune&lt;/item&gt; &lt;/string-array&gt; &lt;/resources&gt;</code></pre> <pre><code>public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String[] planets = getResources().getStringArray(R.array.planets_array); int foundIndex = -1; for (int i = 0; i &lt; planets.length; i++) { String planet = planets[i]; if (planet.equals("Mars")) { foundIndex = i; break; } } String foundPlanet = foundIndex &gt;= 0 ? planets[foundIndex] : "nothing"; ((TextView)findViewById(R.id.found_planet)).setText("found: " + foundPlanet); } }</code></pre> <p>If you'd rather load the data from an xml file in the assets folder, note that it's a bit involved. Take a look at <a href="https://stackoverflow.com/questions/5557885">android: how to load xml file from assets directory?</a>.</p> <p>If the values are just a simple list of strings, then xml structure probably isn't necessary. Here's a simple example of loading the values from a text file in the assets folder.</p> <p><strong>assets/planets.txt</strong></p> <pre><code>Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune</code></pre> <pre><code>public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // String[] planets = getResources().getStringArray(R.array.planets_array); String[] planets = readFromAssetsTxt(); int foundIndex = -1; for (int i = 0; i &lt; planets.length; i++) { String planet = planets[i]; if (planet.equals("Mars")) { foundIndex = i; break; } } String foundPlanet = foundIndex &gt;= 0 ? planets[foundIndex] : "nothing"; ((TextView) findViewById(R.id.found_planet)).setText("found: " + foundPlanet); } String[] readFromAssetsTxt() { try { AssetManager assetManager = getAssets(); List&lt;String&gt; planets = new ArrayList&lt;String&gt;(); BufferedReader in = new BufferedReader(new InputStreamReader(assetManager.open("planets.txt"))); String planet = null; while ((planet = in.readLine()) != null) { planets.add(planet); } in.close(); String[] planetsArray = new String[planets.size()]; planets.toArray(planetsArray); return planetsArray; } catch (IOException e) { e.printStackTrace(); } return new String[] {}; } }</code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload