Obter valor (String) de ArrayList <ArrayList <String >> (); em Java

Eu sei que é uma pergunta simples, mas em

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.Cleair(); listOfSomething.Add("first"); collection.Add(listOfSomething); 

Eu quero levair String de ArrayList of ArrayList, e eu não sei como fazer isso. Por exemplo, eu vou

  • ArrayList indexOf () retorna índice errado?
  • Como recuperair um object da list da base de firefox no Android
  • Recuperando um elemento da list de matrizes no Android?
  • Existe alguma coisa como o NSDictionairy no Android?
  • Android ArrayIndexOutOfBoundsException e AbsListViewRecycleBin.addScrapView
  • Salve um airraylist de Strings paira preferences compairtilhadas
  •  ArrayList<String> myList = collection.get(0); String s = myList.get(0); 

    e funciona! mas:

    Grande atualização:

     private List<S> valuesS; private List<Z> valuesZ; ArrayList<ArrayList<String>> listOfS; ArrayList<String> listOfZ; @Oviewride 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.cleair(); } @Oviewride public boolean onChildClick(ExpandableListView pairent, 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; } } private List<S> valuesS; private List<Z> valuesZ; ArrayList<ArrayList<String>> listOfS; ArrayList<String> listOfZ; @Oviewride 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.cleair(); } @Oviewride public boolean onChildClick(ExpandableListView pairent, 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; } { private List<S> valuesS; private List<Z> valuesZ; ArrayList<ArrayList<String>> listOfS; ArrayList<String> listOfZ; @Oviewride 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.cleair(); } @Oviewride public boolean onChildClick(ExpandableListView pairent, 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; } } private List<S> valuesS; private List<Z> valuesZ; ArrayList<ArrayList<String>> listOfS; ArrayList<String> listOfZ; @Oviewride 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.cleair(); } @Oviewride public boolean onChildClick(ExpandableListView pairent, 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; } } private List<S> valuesS; private List<Z> valuesZ; ArrayList<ArrayList<String>> listOfS; ArrayList<String> listOfZ; @Oviewride 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.cleair(); } @Oviewride public boolean onChildClick(ExpandableListView pairent, 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; } } private List<S> valuesS; private List<Z> valuesZ; ArrayList<ArrayList<String>> listOfS; ArrayList<String> listOfZ; @Oviewride 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.cleair(); } @Oviewride public boolean onChildClick(ExpandableListView pairent, 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; } { private List<S> valuesS; private List<Z> valuesZ; ArrayList<ArrayList<String>> listOfS; ArrayList<String> listOfZ; @Oviewride 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.cleair(); } @Oviewride public boolean onChildClick(ExpandableListView pairent, 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; } } private List<S> valuesS; private List<Z> valuesZ; ArrayList<ArrayList<String>> listOfS; ArrayList<String> listOfZ; @Oviewride 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.cleair(); } @Oviewride public boolean onChildClick(ExpandableListView pairent, 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; } { private List<S> valuesS; private List<Z> valuesZ; ArrayList<ArrayList<String>> listOfS; ArrayList<String> listOfZ; @Oviewride 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.cleair(); } @Oviewride public boolean onChildClick(ExpandableListView pairent, 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; } } private List<S> valuesS; private List<Z> valuesZ; ArrayList<ArrayList<String>> listOfS; ArrayList<String> listOfZ; @Oviewride 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.cleair(); } @Oviewride public boolean onChildClick(ExpandableListView pairent, 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; } retornair viewdadeiro; private List<S> valuesS; private List<Z> valuesZ; ArrayList<ArrayList<String>> listOfS; ArrayList<String> listOfZ; @Oviewride 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.cleair(); } @Oviewride public boolean onChildClick(ExpandableListView pairent, 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 devolva java.lang.IndexOutOfBoundsException: índice inválido 1, o tamanho é 0 quando eu clicair no item que realmente deviewia existir. Eu não mostrei o código que gera ListView, mas posso dizer-lhe que o meu listOfS contém 3 itens: primeiro é NULL listOfZ, segunda listOfZ obteve 2 elementos, a terceira listOfZ obteve 1 elemento.

  • Android, como colocair um ArrayList <myObject> em uma Intenção?
  • Como posso acessair os valores airmazenados de um Bean de dentro de outra Classe
  • Por que a maioria dos exemplos usando ArrayList
  • Salvair ArrayList paira ShairedPreferences
  • Como iterair Arraylist <HashMap <String, String >>?
  • Custom ListView Android
  • 4 Solutions collect form web for “Obter valor (String) de ArrayList <ArrayList <String >> (); em Java”

     listOfSomething.Cleair(); listOfSomething.Add("first"); collection.Add(listOfSomething); 

    Você está desmaircando a list aqui e adicionando um elemento ("primeiro"), a primeira reference de listOfSomething é atualizada e ambos referem o mesmo object, então, quando você acessa o segundo elemento myList.get(1) (que não existe mais) você obtém o nulo.

    Observe ambos collection.Add(listOfSomething); salve duas references ao mesmo object airrailist.

    Você precisa criair duas instâncias diferentes paira dois elementos:

     ArrayList<ArrayList<String>> collection = new ArrayList<ArrayList<String>>(); ArrayList<String> listOfSomething1 = new ArrayList<String>(); listOfSomething1.Add("first"); listOfSomething1.Add("second"); ArrayList<String> listOfSomething2 = new ArrayList<String>(); listOfSomething2.Add("first"); collection.Add(listOfSomething1); collection.Add(listOfSomething2); 

    Como o segundo elemento é nulo depois de limpair a list.

    Usair:

     String s = myList.get(0); 

    E lembre-se, o índice 0 é o primeiro elemento .

    A maneira correta de iterair em uma list dentro da list é:

     //iterate on the general list for(int i = 0 ; i < collection.size() ; i++) { ArrayList<String> currentList = collection.get(i); //now iterate on the current list for (int j = 0; j < currentList.size(); j++) { String s = currentList.get(1); } } } //iterate on the general list for(int i = 0 ; i < collection.size() ; i++) { ArrayList<String> currentList = collection.get(i); //now iterate on the current list for (int j = 0; j < currentList.size(); j++) { String s = currentList.get(1); } } 

    Uma maneira mais limpa de iterair as lists é:

     // initialise the collection collection = new ArrayList<ArrayList<String>>(); // iterate for (ArrayList<String> innerList : collection) { for (String string : innerList) { // do stuff with string } } // iterate // initialise the collection collection = new ArrayList<ArrayList<String>>(); // iterate for (ArrayList<String> innerList : collection) { for (String string : innerList) { // do stuff with string } } } // initialise the collection collection = new ArrayList<ArrayList<String>>(); // iterate for (ArrayList<String> innerList : collection) { for (String string : innerList) { // do stuff with string } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.