how to pass objects between intents

2019-03-02 07:56发布

I have a class that contains data i want to pass it between intents, this class have arraylist that contains another class objects. this is my class

    public class ParsedData implements Parcelable {

        public String error;
        public float protectionLevel;
        public int protectionLevelColor;
        public double lastBackup;
        public boolean compressedType;
        public Long driveFreeSpaceSize;
        ArrayList<Item>Items = new ArrayList<Item>();
}

class Item {

    public String name;
    public float percentage;
    public int files;
    public long size;
}

how can i send this class between intents ?

4条回答
时光不老,我们不散
2楼-- · 2019-03-02 08:43

i have found the answer after all. thanks all how helped me this is the answer:

import android.os.Parcel;
import android.os.Parcelable;

public class ParsedData implements Parcelable  {

    public String error;
    public float protectionLevel;
    public int protectionLevelColor;
    public double lastBackup;
    public boolean compressedType;
    public Long statusSendTime;
    ArrayList<Item>Items = new ArrayList<Item>();

    //---------------------Constructors---------------------------
    public ParsedData() { ; };

    public ParsedData(Parcel in) {
        readFromParcel(in);
    }
    //------------------------------------------------------------

    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(error);
        dest.writeFloat(protectionLevel);
        dest.writeInt(protectionLevelColor);
        dest.writeDouble(lastBackup);
        dest.writeByte((byte) (compressedType ? 1 : 0));  
        dest.writeLong(statusSendTime);
        dest.writeList(Items);

    }

    private void readFromParcel(Parcel in) {
        error = in.readString();
        protectionLevel = in.readFloat();
        protectionLevelColor = in.readInt();
        lastBackup = in.readDouble();
        compressedType =in.readByte() == 1; 
        statusSendTime = in.readLong();
        in.readList(Items,Item.class.getClassLoader() );

    }

     public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
                    public ParsedData createFromParcel(Parcel in) {
                        return new ParsedData(in);
                    }

                    public ParsedData[] newArray(int size) {
                        return new ParsedData[size];
                    }
                };
}

class Item implements Parcelable {

    public String name;
    public float percentage;

    //---------------------Constructors---------------------------
    public Item() {
       }
    public Item(Parcel in) {
          readFromParcel(in);
       }
    //------------------------------------------------------------

    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeFloat(percentage);
    }
    public static final Creator<Item> CREATOR = new Creator<Item>() {
          public Item createFromParcel(Parcel source) {
             return new Item(source);
          }
          public Item[] newArray(int size) {
             return new Item[size];
          }
       };
       private void readFromParcel(Parcel in) {
           this.name = in.readString();
           this.percentage = in.readFloat();
           }
}

and in the caller activity

    ParsedData data = new PArsedData();
    Intent intentBreakDown = new Intent(this,BreakDownBarActivity.class);
    intentBreakDown.putExtra("data", data);
    startActivity(intentBreakDown);

in the called activity(BreakDownBarActivity in my case)

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.breakdownbar);

        Bundle b = getIntent().getExtras();
        ParsedData data = (ParsedData)b.getParcelable("data");
    }
查看更多
放我归山
3楼-- · 2019-03-02 08:52

This may be your problem:

Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface.

Alternatively, I'd try to have Item implement Parcelable, as well.

The fail-safe alternative is to write your data structure into a JSON string, which also allows you to pass the data to other applications that don't have access to your ParsedData class.

查看更多
贼婆χ
4楼-- · 2019-03-02 08:53

you can let your class Item implements the Serializable interface, and use Intent.putExtra(String, Serializable). Since ArrayList implements also the Serializable interface, you can pass the whole Items object.

查看更多
太酷不给撩
5楼-- · 2019-03-02 08:53

You may take a look at Intent.putExtra(String name, Parcelable object) and implement the parcelable interface in your class.

查看更多
登录 后发表回答