Pass string array per parameter to screen2

2

I am following a tutorial on the internet and I have a small question that is the following. In the tutorial, I put items from a String array in a listview and I can select multiple options in simple_list_item_checked.

I'm able to show which ones are selected and those that are not selected, what I needed is to play all the selected ones to another screen, the problem is how to play as String [], because in the example it's just as a String

What I need then is to throw all states for screen2 as the type String []

Does anyone help me? please?

listaEstados = new String[]{"São Paulo", "Rio de Janeiro", "Minas Gerais", "Rio Grande do Sul", "Santa Catarina", "Paraná"};

public void onClickMarcados(View view){
    String listaEstadosSelec = "";

    //Cria um array com os itens selecionados no listview
    SparseBooleanArray selecionados = lv.getCheckedItemPositions();

    for (int i=0; i<selecionados.size(); i++){
        //Pega os itens marcados
        listaEstadosSelec = listaEstadosSelec + listaEstados[selecionados.keyAt(i)]+", ";
    }
    Intent intent = new Intent(MainActivity.this, Tela2_Activity.class);
    intent.putExtra("itens", listaEstadosSelec);
    startActivity(intent);

    //Toast.makeText(getBaseContext(), "Estados marcados: "+listaEstadosSelec, Toast.LENGTH_LONG).show();
}
    
asked by anonymous 16.12.2014 / 19:59

2 answers

4

Do this:

    Intent intent = new Intent(MainActivity.this, Tela2_Activity.class);
    intent.putStringArrayListExtra("itens",new ArrayList<String>( Arrays.asList(listaEstadoSelec)));
    startActivity(intent);

On the other hand, you do:

     ArrayList<String> estados  = getExtras().getStringArrayList("itens");
    
16.12.2014 / 20:09
2

Instead of using a String with comma-separated values, use an ArrayList and pass it using intent.putStringArrayListExtra() :

listaEstados = new String[]{"São Paulo", "Rio de Janeiro", "Minas Gerais", "Rio Grande do Sul", "Santa Catarina", "Paraná"};

public void onClickMarcados(View view){

    //ArrayList para receber os itens selecionados
    ArrayList<String> listaEstadosSelec = new ArrayList<String>();

    //Cria um array com os itens selecionados no listview
    SparseBooleanArray selecionados = lv.getCheckedItemPositions();

    for (int i=0; i<selecionados.size(); i++){

        //Pega os itens marcados e adiciona ao ArrayList
        listaEstadosSelec.add(listaEstados[selecionados.keyAt(i)]);
    }
    Intent intent = new Intent(MainActivity.this, Tela2_Activity.class);

    //Usa o putStringArrayListExtra() para passar o ArrayList
    intent.putStringArrayListExtra("itens", listaEstadosSelec);
    startActivity(intent);
}
    
16.12.2014 / 20:30