Pass String array by parameter to tela2

Asked

Viewed 548 times

2

I am following a tutorial on the Internet and I came up with a small doubt that is the following. In the tutorial, I place items from a String array in a listview and in it I can select various options by the simple_list_item_checked.

I’m managing to show which are selected and which are not selected, what I needed is to play all selected to another screen, the problem is how to play as String[], because in the example it is only as a String

What I need then is to play all states to the tela2 as the String type[]

Can I get some help? Lease?

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();
}

2 answers

4


Do so:

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

On the other side, you do:

     ArrayList<String> estados  = getExtras().getStringArrayList("itens");
  • It did not work very well, no friend, from the following error: The method putStringArrayListExtra(String, Arraylist<String>) in the type Intent is not applicable for the Arguments (String, List<String>)

  • Ah tah, pardon , I will correct in response

  • It is because the Arrays.asList method returns a List and not an Arraylist. Then, just make a new instance of Arraylist by placing the type (important in this case) and passing the List back to the constructor.

  • Roger that, it worked out, buddy. Thanks for your help!

2

Instead of using a String with values separated by comma, use a 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);
}
  • Very good @ramaral, this is exactly what I needed, not discarding the other good help... ^^

Browser other questions tagged

You are not signed in. Login or sign up in order to post.