How to send data from a Vector to another Activity?

5

I'm a beginner, and I have the following question: I'm inserting text from a EditText into a array , and I want to get it in another. I'm doing the following:

String[] pedidos = new String[11];

And then, when you click the "save" button, this happens:

    public void onClick(View v) {
    if(v.getId() == R.id.btnFinalizar){
        pedidos[0] = edtPedido.getText().toString(); //insiro dados do EditText em uma array.
        Intent intent = new Intent(mesa1.this, inicio.class);
        intent.putExtra("pedidos1", pedidos[0]);
        startActivity(intent);
    }
}

And on the screen where I want to receive the data, I do this:

        String[] pedidosFeitos = new String[11];

    Intent intent = getIntent();
    pedidosFeitos[0] = String.valueOf(intent.getStringArrayExtra("pedidos1"));

Note: I did not understand: String.valueOf(intent.getStringArrayExtra

I researched to the fullest, and ended up trying what Android Studio suggested.

    
asked by anonymous 25.11.2015 / 07:01

1 answer

5

When doing this intent.putExtra("pedidos1", pedidos[0]); , you are not passing an array (vector in your case), but a string, after all, pedidos is a vector of strings and pedidos[0] is the string that is in position 0 of the vector.

In addition, when declaring intent.getStringArrayExtra("pedidos1") you are trying to get a string as if it were an array, which is not the case.

Change your code to:

if(v.getId() == R.id.btnFinalizar){
        pedidos[0] = edtPedido.getText().toString(); //insiro dados do EditText em uma array.
        Intent intent = new Intent(mesa1.this, inicio.class);
        intent.putExtra("pedidos1", pedidos);
        startActivity(intent);
    }

In the other Activity :

Intent intent = getIntent();
 String[] pedidosFeitos = intent.getStringArrayExtra("pedidos1"));

Now, if your goal is to pass only one order (index of the requested array), the way your code already is, just change the getStringArrayExtra to just getStringExtra .

More information can be found in the android documentation > and I also recommend you take a look at Getting Started android, it's in English, but it helps a lot for those starting out.

PS: String.valueOf () returns one string representation of the argument passed, but in this your code is not very necessary.

Reference:

link

link

    
25.11.2015 / 10:33