Apr 22, 2011

Load Array from XML Resources

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

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"?>
<resources>
<array name="month">
<item>January</item>
<item>February</item>
<item>March</item>
<item>April</item>
<item>May</item>
<item>June</item>
<item>July</item>
<item>August</item>
<item>September</item>
<item>October</item>
<item>November</item>
<item>December</item>
</array>
</resources>


package com.ExMyList;

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

public class ExMyList extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(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

Infolinks In Text Ads