So che è semplice domanda, ma inOttieni valore (stringa) di ArrayList <ArrayList <String>>(); in Java
ArrayList<ArrayList<String>> collection;
ArrayList<String> listOfSomething;
collection= new ArrayList<ArrayList<String>>();
listOfSomething = new ArrayList<String>();
listOfSomething.Add("first");
listOfSomething.Add("second");
collection.Add(listOfSomething);
listOfSomething.Clear();
listOfSomething.Add("first");
collection.Add(listOfSomething);
voglio prendere String da ArrayList di ArrayList, e non so come fare. Per esempio vado
ArrayList<String> myList = collection.get(0);
String s = myList.get(0);
e funziona! ma:
Big aggiornamento:
private List<S> valuesS;
private List<Z> valuesZ;
ArrayList<ArrayList<String>> listOfS;
ArrayList<String> listOfZ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Zdatasource = new ZDataSource(this);
Zdatasource.open();
valuesZ = Zdatasource.getAllZ();
Sdatasource = new SDataSource(this);
Sdatasource.open();
valuesS = Sdatasource.getAllS();
List<Map<String, String>> groupData
= new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData
= new ArrayList<List<Map<String, String>>>();
listOfS = new ArrayList<ArrayList<String>>();
listOfZ = new ArrayList<String>();
for (S i : valuesS) { // S is class
for (Z j : valuesZ) { // Z is class
if(j.getNumerS().equals(i.getNumerS())) {
listOfZ.add(j.getNumerZ());
}
else
{
//listOfZ.add("nothing");
}
}
listOfS.add(listOfZ);
if(!listOf.isEmpty()) listOfZ.clear();
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
try
{
ArrayList<String> myList = listOfS.get(groupPosition);
String s = myList.get(childPosition);
PrintToast("group "+Integer.toString(groupPosition)+", child "+Integer.toString(childPosition) + " , "+ s);
}
catch(Exception e)
{
Log.e("FS", e.toString());
}
return true;
}
me
ritorno java.lang.IndexOutOfBoundsException: Indice non valido 1, la dimensione è 0 quando clicco sulla voce che in realtà dovrebbe esistere. Non ho mostrato il codice che genera ListView, ma posso dirti che il mio listOfS contiene 3 elementi: prima è Null listOfZ, il secondo listOfZ ha 2 elementi, il terzo listOfZ ha 1 elemento.
Si ottiene nulla su cui chiamare - a myList o String s? – drewmoore
Se hai null, vuol dire che il secondo elemento del primo elenco nella raccolta è nullo. Come potremmo aiutare? –
Bene, ho intenzione di aggiornare la mia domanda. – boski