Load string array value from strings.xml : Array « Date Type « Android






Load string array value from strings.xml

  

package app.test;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Test extends ListActivity {
  String[] presidents;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ListView lstView = getListView();
    // lstView.setChoiceMode(0); //CHOICE_MODE_NONE
    // lstView.setChoiceMode(1); //CHOICE_MODE_SINGLE
    lstView.setChoiceMode(2); // CHOICE_MODE_MULTIPLE
    lstView.setTextFilterEnabled(true);

    presidents = getResources().getStringArray(R.array.presidents_array);

    setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_checked, presidents));
  }

  public void onListItemClick(ListView parent, View v, int position, long id) {
    parent.setItemChecked(position, parent.isItemChecked(position));

    Toast.makeText(this, "You have selected " + presidents[position],
        Toast.LENGTH_SHORT).show();
  }
};

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    

         
</LinearLayout>
//strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">BasicViews5</string>
    <string-array name="presidents_array">
        <item>Dwight D. Eisenhower</item>
        <item>John F. Kennedy</item>
        <item>Lyndon B. Johnson</item>
        <item>Richard Nixon</item>
        <item>Gerald Ford</item>
        <item>Jimmy Carter</item>
        <item>Ronald Reagan</item>
        <item>George H. W. Bush</item>
        <item>Bill Clinton</item>
        <item>George W. Bush</item>
        <item>Barack Obama</item>
    </string-array>
</resources>

   
    
  








Related examples in the same category

1.Returns a clone of the specified array.
2.Tests two arrays for equality.
3.Transforms a list of Float values into an array of float.
4.Split the string into an array of strings using one of the separator in 'sep'.
5.Int array to HashSet
6.Array Iterator
7.Convert an array of floats to 16.16 fixed-point
8.Convert an array of 16.16 fixed-point values to floating point
9.Reverse an array
10.Using android.util.SparseArray
11.Join an array of Strings together.
12.Compare Arrays
13.Does Array contain a certain item
14.Create Array with Unique Value
15.Resize a Java array
16.Get Node value and save to ArrayList
17.read the photo file into a byte array
18.byte Array To Int
19.load 16 Bit PCM Raw Data File As Double Array
20.convert From Short Array To Double Array
21.convert From Double Array To Short Array
22.get Set From Array
23.Convert an array of floats to 16.16 fixed-point 2
24.int To Byte Array 4
25.Convert a byte array to a boolean array. Bit 0 is represented with false, Bit 1 is represented with 1
26.Return a subarray of the byte array in parameter.