How to send data from one Vector to another Activity?

Asked

Viewed 889 times

5

I’m still a beginner, and I have the following question: I’m inserting text from a EditText in a array, and I want to receive it in another. I am doing the following:

String[] pedidos = new String[11];

And then, by clicking on 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 the: String.valueOf(intent.getStringArrayExtra

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

  • In the case of your implementation you are passing item by item of an array. Sending: intent.putExtra("pedidos1", pedidos[0]); and receiving pedidosFeitos[0] = String.valueOf(intent.getStringArrayExtra("pedidos1"));. Along those lines String.valueOf(intent.getStringArrayExtra("pedidos1")); will be converting a value (which should be an Array and not an Array item) to String.

  • A question: You want to send only one item at a time or all items at a time to another Activity?

  • Man, sorry for the error, I ended up confusing array with vector, thanks for the correction. With your answer, and with the friend below, I managed to settle here, thank you very much.

1 answer

5

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

Moreover, when declaring intent.getStringArrayExtra("pedidos1") you are trying to pick up 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 request(Input from the requested array), as your code already stands, just change the getStringArrayExtra to just getStringExtra.

More information can be found on android documentation and I also recommend that you take a look at Getting Started android, is in English, but helps a lot for those who are starting.

P.S.: String.valueOf() returns a string representation of the argument passed, however, in this your code is not very necessary.

Reference:

https://stackoverflow.com/a/8892968/5524514

http://developer.android.com/intl/pt-br/guide/index.html

  • Our guy, sorry, I misspelled, and I ended up confusing the between vector and array. But I’m glad they fixed my mistake, and helped me. Thank you very much, man.

  • 1

    @Nilzonmartins worked out the solution?

  • No. didn’t work. The bug app, as soon as it starts, automatically closes.

  • 1

    @Nilzonmartins then the problem is probably not that of the question. Check logcat for errors that are released when the app suddenly closes.

Browser other questions tagged

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