Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When I need custom objects that can be edited outside code i generally use json which is easier to read for both humans and (possibly) machines ;) </p> <p>You can also have more complex objects than with simple arrays.</p> <p>Once you create a json file (e.g. countries.json) in the <code>/res/raw</code> folder like this:</p> <pre><code>{ "countries" : [ {"country" : "Albania", "countryCode" : "al" }, {"country" : "Algeria", "countryCode" : "dz"}, {"country" : "American Samoa", "countryCode" : "as"}, {"country" : "India", "countryCode" : "in"}, {"country" : "South Africa", "countryCode" : "sa"} ]} </code></pre> <p>you can load the data like this:</p> <pre><code>InputStream jsonStream = context.getResources().openRawResource(R.raw.countries); JSONObject jsonObject = new JSONObject(Strings.convertStreamToString(jsonStream)); JSONArray jsonContries = jsonObject.getJSONArray("countries"); List&lt;CountryVO&gt; countries = new ArrayList&lt;CountryVO&gt;(); for (int i = 0, m = countries.length(); i &lt; m; i++) { JSONObject jsonCountry = countries.getJSONObject(i); CountryVO country = new CountryVO(); country.setCountryName(jsonCountry.getString("country")); String co = jsonCountry.getString("countryCode"); country.setCountryCode(co); try { Class&lt;?&gt; drawableClass = com.example.R.drawable.class; // replace package Field drawableField = drawableClass.getField(co); int drawableId = (Integer)drawableField.get(null); Drawable drawable = getResources().getDrawable(drawableId); country.setCountryFlag(drawable); } catch (Exception e) { // report exception } countries.add(country); } </code></pre> <p>If you don't want to do the parsing manually you can also use <a href="http://code.google.com/p/google-gson/" rel="nofollow noreferrer">gson</a> which helps you to pass the objects and then load the drawables in a lazy fashion... ;)</p> <p><strong>Edit:</strong> Added utility class</p> <pre><code>public String convertStreamToString(InputStream is) { Scanner s = new Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } </code></pre> <p>Hope it helps</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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