Saturday, July 21, 2012

Load Array from XML Resources

To load array contents from XML resources:
 String[] month = getResources().getStringArray(;

where xxx is the name of the array.

Here is a example of ListActivity, using ArrayAdapter<String> with array contents loaded from XML resources.

create a xml file, /res/values/myvalues.xml
<?xml version="1.0" encoding="UTF-8"?>
<array name="month">

package com.ExMyList;

import android.os.Bundle;
import android.widget.ArrayAdapter;

public class ExMyList extends ListActivity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        String[] month = getResources().getStringArray(R.array.month);
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, month));

No comments:

Post a Comment