【问题标题】:Passing and ArrayList<Service> through intent通过意图传递和 ArrayList<Service>
【发布时间】:2023-03-07 02:47:01
【问题描述】:

我有一个叫Service的类,用来使用这个构造函数创建Service对象

public Service(int id, String service_name, String service_code) {
    this.id = id;
    this.service_name = service_name;
    this.service_code = service_code;
}

然后我使用以下签名创建一个列表调用服务列表

List<Service> serviceList = new ArrayList<Service>

我尝试通过 Intent 像这样的对象传递这个 ArrayList

Intent i = new Intent(Classname.this, anotherClass.class);
i.putExtras("serviceList",serviceList);
startActivity(i);

但它失败了。我通过 ArrayList 对象传递意图的方式是什么。

【问题讨论】:

    标签: android android-intent arraylist


    【解决方案1】:

    您的自定义类必须实现 ParcelableSerializable 才能在 Intent 中序列化/反序列化。

    例如,您的课程 Service 必须看起来像这样(使用生成器 http://www.parcelabler.com/

    public class Service implements Parcelable {
    private int id;
    private String service_name;
    private String service_code;
    public Service(int id, String service_name, String service_code) {
    this.id = id;
    this.service_name = service_name;
    this.service_code = service_code;
    }
    
    
    protected Service(Parcel in) {
        id = in.readInt();
        service_name = in.readString();
        service_code = in.readString();
    }
    
    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(service_name);
        dest.writeString(service_code);
    }
    
    @SuppressWarnings("unused")
    public static final Parcelable.Creator<Service> CREATOR = new Parcelable.Creator<Service>() {
        @Override
        public Service createFromParcel(Parcel in) {
            return new Service(in);
        }
    
        @Override
        public Service[] newArray(int size) {
            return new Service[size];
        }
    };
    

    }

    然后你可以使用getIntent().getParcelableArrayListExtra() 进行强制转换

    ArrayList<Service> serviceList= intent.<Service>getParcelableArrayList("list"));
    

    这样发送给你

    intent.putParcelableArrayListExtra("list", yourServiceArrayList);
    

    注意yourServiceArrayList 应该是一个ArrayList

    如果是List你可以通过

    intent.putParcelableArrayListExtra("list", (ArrayList<? extends Parcelable>) yourServiceArrayList);

    【讨论】:

    • 我如何发送 Parcalable Intent
    • @nifCody 基本上是这样的intent.putParcelableArrayListExtra("list", yourServiceArrayList)
    • 我们需要转换 yourServiceArrayList
    • intent.putParcelableArrayListExtra("serviceCard", (ArrayList extends Parcelable>) serviceCart);
    • @nifCody 不,你没有。您是否像我提供的那样更改了 Service 课程?
    【解决方案2】:

    'Service'类可以使用parcelable接口,通过

    发送对象

    intent 使用 'putParcelableArrayListExtra' 方法并检索您可以使用的数据

    'getParcelableArrayListExtra'

    供您参考 refer this 链接

    【讨论】:

      【解决方案3】:

      使用 Serializable 实现对象类。 例如。

      class abc implements Serializable{
      //your code
      }
      

      然后试试这段代码

      ArrayList<abc> fileList = new ArrayList<abc>();
      Intent intent = new Intent(MainActivity.this, secondActivity.class);
       intent.putSerializable("arraylisty",filelist);
      startActivity(intent);
      

      在另一边接收类似的意图

      your arraylist objact=intent.getSerializableExtra(String name)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-22
        • 2015-03-17
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多