2013-07-29 26 views
5

ho dati recuperati dal mio webservice inArrayList <HashMap <String, String >> per String []

ArrayList<HashMap<String,String>> 

Ora voglio convertire ogni oggetto della sopra per

String[] 

come faccio questo? qualsiasi aiuto sarebbe molto apprezzato!

+0

contiene i dati ottenuti da un webservice che serve dati JSON. Ho bisogno di estrarre ogni campo come un array di stringhe per popolarle in viste diverse come viwepagers e viste elenco. –

+1

http://stackoverflow.com/questions/1090556/java-how-to-convert-hashmapstring-object-to-array.check questo potrebbe aiutare. – Raghunandan

+0

Nell'array si desidera inserire le chiavi, i valori o entrambi di hashmap? – Alberto

risposta

6

provare

ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>(); 
HashMap<String, String> n = new HashMap<String, String>(); 
n.put("a", "a"); 
n.put("b", "b"); 
test.add(n); 

HashMap<String, String> m = test.get(0);//it will get the first HashMap Stored in array list 

String strArr[] = new String[m.size()]; 
int i = 0; 
for (HashMap<String, String> hash : test) { 
    for (String current : hash.values()) { 
     strArr[i] = current; 
     i++; 
    } 
} 
1

Gli usi per una mappa hash dovrebbero essere un indice di valori hash per trovare i valori molto più velocemente. Non so il motivo per cui si dispone di chiave e valori come stringhe, ma se avete solo bisogno i valori che si possa fare in quel modo:

ArrayList<HashMap<String, String>> test = new ArrayList<>(); 
String sum = ""; 
for (HashMap<String, String> hash : test) { 
    for (String current : hash.values()) { 
     sum = sum + current + "<#>"; 
    } 
} 
String[] arr = sum.split("<#>"); 

Non è un bel modo, ma la richiesta non è troppo;)

+0

grazie Funziona :) –

0
ArrayList<HashMap<String, String>> meterList = controller.getMeter(); 

HashMap<String, String> mtiti = meterList.get(0);//it will get the first HashMap Stored in array list 

String[] strMeter = new String[mtiti.size()]; 

String meter = ""; 
for (HashMap<String, String> hash : meterList) { 
    for (String current : hash.values()) { 
     meter = meter + current + "<#>"; 
    } 
} 
String[] arr = meter.split("<#>"); 
Problemi correlati