0

I've got some issues while try to send information from an activity to an other. I want to send some custom object. I load them in my first activity, because of optimisation, but now i want to get them in the activity that will use them so my idea was to put extras and get thoses extras BUT i can't get them because i don't really know how to use put extra for custom methods

Here is my object :

public class VMyCode{

  private String name;
  private ArrayList<GeneticStep> code;
  private int image;

  public VMyCode(){
      this.name = null;
      this.code = null;
      this.image = -1;
  }

  public VMyCode(String name, ArrayList<GeneticStep> code, int image){
      this.name = name;
      this.code = code;
      this.image = image;
  }

  public int getImage() {
      return image;
  }

  public String getName() {
      return name;
  }

  public ArrayList<GeneticStep> getCode() {
      return code;
  }

  public void setName(String name) {
      this.name = name;
  }

  public void setCode(ArrayList<GeneticStep> code) {
      this.code = code;
  }

  public void setImage(int image) {
      this.image = image;
  }
}

What i want to do is send from the first activity an ArrayList of VMyCode and get it in the other activity.

I've tried to make my object implements Serializable, and getSerializableExtras casted into an ArrayList, but don't looks like it's working.

If someone has some idea, feel free to share ! Thanks

Ps : Sorry for my english.

Nico
  • 168
  • 2
  • 12

4 Answers4

1

One proper way of doing this is by implementing Parcelable in your class. This answer shows how to implement it:

https://stackoverflow.com/a/7181792/2534007

You can do this manually as explained in the answer above or you can use this http://www.parcelabler.com/ which directly provides implementation of Parcelable.

After that, you can pass your object as extra through intent.

Community
  • 1
  • 1
Mohib Irshad
  • 1,940
  • 23
  • 18
  • Well thanks very usefull, do the stuff i want to ! A liltle question remains : my custom object VMyCode use GeneticStep which is a custom object, should it implements Parcelable too? And if it does, should all my custom objects use in objects that may be use by VMyCode or GeneticStep implements Parcelable ? – Nico Apr 27 '15 at 07:05
  • Yes, you have to implement Parcelable in those classes. Please refer to this question, it will help you in implementing list with custom parcelable classes: http://stackoverflow.com/questions/14178736/how-to-make-a-class-with-nested-objects-parcelable – Mohib Irshad Apr 27 '15 at 07:34
0

make the core or bean classes as parcelable so that you can send the objects between the components.

here is the example parcelable example

Rathan Kumar
  • 2,567
  • 2
  • 17
  • 24
0

implement Parcelable and pass the custom object using Intents.

Shriram
  • 4,343
  • 8
  • 37
  • 64
0

You can use parceble: as there is done previously like:

setclass d = new setclass ();
                d.setDt(5);
                LinkedHashMap<String, Object> obj = new LinkedHashMap<String, Object>();
                obj.put("hashmapkey", d);
            Intent inew = new Intent(SgParceLableSampelActivity.this,
                    ActivityNext.class);
            Bundle b = new Bundle();
            b.putSerializable("bundleobj", obj);
            inew.putExtras(b);
            startActivity(inew);

And for getting values in another activity:

try {  setContentView(R.layout.main);
            Bundle bn = new Bundle();
            bn = getIntent().getExtras();
            HashMap<String, Object> getobj = new HashMap<String, Object>();
            getobj = (HashMap<String, Object>) bn.getSerializable("bundleobj");
            setclass  d = (setclass) getobj.get("hashmapkey");
        } catch (Exception e) {
            Log.e("Err", e.getMessage());
        }
kiturk3
  • 549
  • 10
  • 30