Pass Volley api data to another screen

Asked

Viewed 57 times

0

I have an application that gets given via api using Volley

My code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tela1);

        ImageView myImageView = (ImageView) findViewById(R.id.imageView);
        myImageView.setImageResource(R.drawable.imagem);

        requestQueue = Volley.newRequestQueue(this);


        results = (TextView) findViewById(R.id.textView2);
        datav = (TextView) findViewById(R.id.textA);



        JsonArrayRequest arrayreq = new JsonArrayRequest(JsonURL,

                new Response.Listener<JSONArray>() {

                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            JSONObject obj = response.getJSONObject(0);
                            Double ask = obj.getDouble("ask");
                            String data = obj.getString("create_date");

                            results.setText("Vale R$: " + ask);
                            datav.setText("Atualizado em: " + data);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }}},

                new Response.ErrorListener() {
                    @Override
                    // Handles errors that occur due to Volley
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Volley", "Error");
                    }
                }
        );

I want to pass the value of "Ask" which is received to another screen, how do I do this?

1 answer

2

If starting a new Activity you can just put as Extra in the Intent

 Intent intent = new Intent(this,NovaActivity.class);
 intent.putExtra("ask",ask);
 startActivity(intent);

and to recover in the other

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getIntent().getDoubleExtra("ask");
}
  • I can only get the value of "Ask" from inside: @Override public void onResponse(Jsonarray Response) { Try { } in this case as it would be?

  • then you’ll do the same thing, only inside the onResponse.

Browser other questions tagged

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