Send variables to another Android Studio activity

1

I am programming an App in which I want that by pressing a button I send variables to an Activity but without opening the next activity, since it is like a sales cart type where the products are acomulated.

I want the activity to open when the user wants.

Thanks

public void pancakes(View v){
    control+=1;
    pedido.setText(control+"");
    Toast t = Toast.makeText(getApplicationContext(),"Producto agregado", Toast.LENGTH_SHORT);
    t.show();
    String nombre = "Pancakes";
    double precio=60.00;
}

This is the code of my button, I define it with onClick, and the last two variables (name, price) are what I want to send to the activity but if I open it.

And so I have several buttons and what I want is to accumulate the information and then show it in another activity

    
asked by J. Torres 14.10.2016 в 17:50
source

3 answers

1

What I recommend to you according to what you want is this

public class Producto implements Serializable 
public class Producto(){
    String nombre;
    double precio;

   //Aqui haces los constructores y get and set
}

public class MainActivity extends Activity {
    List<Producto> productos = new ArrayList<Producto>();
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      //Tu codigo
    }
    //Me imagino que la funcion de tu boton cuando agrega el producto
    Button addProducto= (Button) findViewById(R.id.addProducto);
    addProducto.setOnClickListener( new OnClickListener() {

        @Override
        public void onClick(View v) {
            Producto producto = new Producto("Producto prueba", 60.00);
            productos.add(producto);
            Toast t = Toast.makeText(getApplicationContext(),"Producto agregado", Toast.LENGTH_SHORT);
            t.show();
        }
    });

    //boton para ver el carrito
    Button verCarrito= (Button) findViewById(R.id.verCarrito);
    verCarrito.setOnClickListener( new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getBaseContext(), Carrito.class);
            intent.putExtra("Productos", productos);
            startActivity(intent)
        }
    });
}

In the other activity

public class CarritoActivity extends Activity {
    List<Producto> productos = new ArrayList<Producto>();
    public void onCreate(Bundle savedInstanceState) {
        productos =(ArrayList<Producto>)getIntent().getSerializableExtra("Productos");
    }
}
    
answered by 14.10.2016 / 18:13
source
0

You do not need to send an activity, you can overwrite the Application instance of your application and there you can enter the data you want, like your shopping cart, you can see how in this link

    
answered by 19.10.2016 в 22:59
0

What you want is to send objects between Activities, in this case objects Product in an ArrayList, is done in this way, you create your ArrayList of objects and send it in Intent by .putExtra() :

   Intent intent = new Intent(MainActivity.this, SegundaActivity.class);
                intent.putExtra("Productos", productos);
                startActivity(intent);

To receive the ArrayList of Product objects in the Activity, it is done in the following way:

ArrayList<Producto> lista = (ArrayList<Producto>) getIntent().getSerializableExtra("Productos");

However, there is something very important that you need to do this and that is that your Product object should implement the class Serializable :

public class Producto implements Serializable {
    
answered by 15.10.2016 в 06:36