Sto solo cercando di visualizzare un elenco da un array che ho nel mio array.xml. Quando provo a eseguirlo nell'emulatore, ricevo un messaggio di chiusura forzata.Aiuto per ottenere Array di stringhe dal file arrays.xml
Se io definisco la matrice nel file java
String[] testArray = new String[] {"one","two","three","etc"};
funziona, ma quando uso
String[] testArray = getResources().getStringArray(R.array.testArray);
doesnt lavoro.
Ecco il mio file Java:
package com.xtensivearts.episode.seven;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class Episode7 extends ListActivity {
String[] testArray = getResources().getStringArray(R.array.testArray);
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ArrayAdapter that will contain all list items
ArrayAdapter<String> adapter;
/* Assign the name array to that adapter and
also choose a simple layout for the list items */
adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
testArray);
// Assign the adapter to this ListActivity
setListAdapter(adapter);
}
}
Ecco il mio file arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="testArray">
<item>first</item>
<item>second</item>
<item>third</item>
<item>fourth</item>
<item>fifth</item>
</array>
</resources>
Che lo ha fatto! Grazie mille per il tuo aiuto e la tua spiegazione. Essendo un principiante ho bisogno delle spiegazioni! – Soren
Salvato la mia giornata! Grazie! : D – borislemke
Vorrei anche aggiungere che "-" non sono consentiti nei nomi di array. Eclipse non mi ha avvisato, il file xml sembrava a posto, e mi ci è voluto un po 'per capire che questo stava causando il problema. "_" funziona bene. – Lesik2008