Pass and get custom class parameters to Intent

0

Hi guys, this time I would like to ask you in what way I pass and I receive parameters from a custom class that I created at an intent

for example:

Intent i = new Intent(this, ValidarCumplimiento.class );

Vehiculo el_vehiculo = new Vehiculo("ASD384", true, 1, 45);
i.putExtra("vehiculo", el_vehiculo );
startActivity(i);

capture it (I do not know how to do it):

Bundle params = getIntent().getExtras();
hora = params.getString("vehiculo");

in advance, thank you very much for your collaboration, which is so useful!

    
asked by Felipe Mendieta Perez 15.02.2017 в 16:08
source

2 answers

5

I see that you are placing an object and then you are trying to get it as string params.getString ("vehicle"); which is incorrect.

To be able to do this, I can suggest you implement Serializable in the Vehicle class, which will allow you to pass an object from one activity to the other.

Example

For your vehicle class

public class Vehiculo implements Serializable {

}

To place the value in the Intent

Intent i = new Intent(this, ValidarCumplimiento.class );

Vehiculo el_vehiculo = new Vehiculo("ASD384", true, 1, 45);
i.putExtra("vehiculo", el_vehiculo );
startActivity(i);

To get the object from the Validate Compliance

activity
Bundle extras = getIntent().getExtras();

if(extras != null) {
  Vehiculo vehiculo = (Vehiculo) extras.getSerializable("vehiculo");
}
    
answered by 15.02.2017 / 16:23
source
1

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

   Intent intent = new Intent(MainActivity.this, ValidarCumplimiento.class);
   intent.putExtra("vehiculo", vehiculo);
   startActivity(intent);

To receive the Object in the Activity, it is done in the following way:

Vehiculo  vehiculo = (Vehiculo) getIntent().getSerializableExtra("vehiculo");

It is very important that in order to do this your Vehicle object must implement the class Serializable :

public class Vehiculo implements Serializable {
    
answered by 15.02.2017 в 16:23