String-arrays de referencia en android strings.xml
Me gustaría hacer referencia a una matriz de cadenas de otra matriz de cadenas en strings.xml. Si intento leer la matriz de cadenas (denominada "plantas") en mi actividad, el valor de cada elemento es nulo.
¿Existe la posibilidad de obtener estos valores?
- Cambiar android: Theme.Dialog para encender el cuadro de diálogo de AppCompat
- Xml spring para android con gradle
- RadioGroup checkedButton propiedad
- Android: ¿Cómo incluir un menú xml dentro de otro menú xml?
- ¿Cómo aplicar dos estilos diferentes a un elemento en android?
Esta es la parte de strings.xml:
<string name="Orlaya_grandiflora_1">Orlaya grandiflora</string> <string name="Orlaya_grandiflora_2">Large-Flowered Orlaya</string> <string name="Orlaya_grandiflora_3">Apiaceae</string> <string-array name="Orlaya_grandiflora"> <item>@string/Orlaya_grandiflora_1</item> <item>@string/Orlaya_grandiflora_2</item> <item>@string/Orlaya_grandiflora_3</item> </string-array> <string-array name="plants"> <item>@array/Ginkgo_biloba</item> <item>@array/Capsicum_frutescens</item> <item>@array/Viscum_album</item> <item>@array/Orlaya_grandiflora</item> </string-array>
Trato de acceder a los valores como este, por ejemplo:
String[] plantArray = resources.getStringArray(R.array.plants); for (String plant : plantArray) { System.out.println("--> " + plant); }
Los valores de la planta son en todos los casos "nulos"
¿Alguien sabe cómo acceder a los valores?
- El contenido de los elementos debe consistir en datos de caracteres bien formados o marcado. AndroidManifest.xml
- No puedo desplazar mi diseño en multi-ventana en Nougat 7.0
- Android tamaño de la barra de clasificación (no puede utilizar Widget.RatingBar.Small)
- Simple Xml - orden de elementos no conservados?
- Pantalla Android Parpadea en mostrar / ocultar barras del sistema
- Cómo llamar a strings.xml en arraylist
- La ventana emergente AutoCompleteTextView de Android se mueve tras mostrar
- Mejor manera de analizar la Biblia en Android
Debe hacer lo siguiente:
//obtain the array that refrences others array TypedArray plantArray=getResources().obtainTypedArray(R.array.plants); //obtain the referenced arrays CharSequence[] ginkoArray=plantArray.getTextArray(0); //... CharSequence[] orlayaArray=plantArray.getTextArray(3);
Ejemplo recto dado en: Android - String Resources
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 HW Bush</item> <item>Bill Clinton</item> <item>George W. Bush</item> <item>Barack Obama</item> </string-array> </resources>
- JQuery Mobile no funciona en WebView al cargar desde activos locales
- Mostrar alerta de diálogo de una clase que no sea de actividad en android