【问题标题】:how to pass objects between intents如何在意图之间传递对象
【发布时间】:2012-05-13 13:10:03
【问题描述】:

我有一个包含我想在意图之间传递它的数据的类,这个类有包含另一个类对象的 arraylist。这是我的课

    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;
}

如何在意图之间发送此类?

【问题讨论】:

  • 你应该阅读mobileorchard.com/…。关于 Intents 的一些信息,并传递了额外的数据。还有一点关于使用数据和从 Intent 返回数据。
  • Item中实现Parcelable有帮助吗?

标签: android android-intent parcelable


【解决方案1】:

您可以让您的class Item 实现Serializable 接口,并使用Intent.putExtra(String, Serializable)。由于ArrayList 也实现了Serializable 接口,您可以传递整个Items 对象。

【讨论】:

    【解决方案2】:

    这可能是你的问题:

    实现 Parcelable 接口的类还必须有一个名为 CREATOR 的静态字段,这是一个实现 Parcelable.Creator 接口的对象。

    或者,我也会尝试让Item 实现Parcelable

    故障安全的替代方法是将您的数据结构写入 JSON 字符串,这还允许您将数据传递给无权访问您的 ParsedData 类的其他应用程序。

    【讨论】:

    • 非常感谢,其实这是我的问题,我让 Item 类实现 Parcelable
    【解决方案3】:

    你可以看看 Intent.putExtra(String name, Parcelable object) 并在你的类中实现 parcelable 接口。

    【讨论】:

    • 我无法打包数组列表,这是我的问题
    • 另一种解决方案是您可以使用数组列表创建一个静态类,然后您可以将其传递给另一个应用程序而不实现 Parcelable 。但这不是一个好习惯
    【解决方案4】:

    毕竟我找到了答案。谢谢所有对我的帮助 这就是答案:

    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();
               }
    }
    

    在调用者活动中

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

    在被调用的活动中(在我的例子中是 BreakDownBarActivity)

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.breakdownbar);
    
            Bundle b = getIntent().getExtras();
            ParsedData data = (ParsedData)b.getParcelable("data");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 2013-09-02
      相关资源
      最近更新 更多