【问题标题】:How do I pass arraylist of a custom object from one java class to another? [duplicate]如何将自定义对象的数组列表从一个 java 类传递到另一个? [复制]
【发布时间】:2023-03-29 21:53:01
【问题描述】:

我有一个Arraylist 的自定义对象。我正在尝试将它从一个 java 文件传递​​到另一个。我尝试了putExtra 方法和Parcelable 选项;但我无法理解Parcelable

这是我的自定义对象:

public class AddValues implements Serializable{
    public int id;
    String value;

    public AddValues(int id, String value)
    {
        this.id = id;
        this.value = value;
    }

    @Override
    public String toString() {
        String result = "id = "+id+","+" value = "+value;
        return result;
    }

    public  int getid()
    {
        return  this.id;
    }

    public String getvalue()
    {
        return this.value;
    }
}

这是我发送ArrayList的代码:

Intent intent = new Intent(BluetoothLeService.this,HomePageFragment.class);
intent.putExtra("id", data_id);
intent.putExtra("value", list);

这里的“列表”指的是ArrayList

【问题讨论】:

  • 你有什么问题?显示您从意图中获取list 的代码。

标签: android arraylist


【解决方案1】:

SerializableParcelable 不是同一个东西,尽管它们的用途相同。这个post 包含一个 Parcelable 对象的示例。

您要创建的所有 Parcelable 对象都应遵循所采用的模式,仅更改 writeToParcelAddValues(Parcel in) 方法。这两个方法应该相互镜像,如果writeToParcel 写入int 然后是string,构造函数应该读取int 然后是string

可打包

public class AddValues implements Parcelable{
    private int id;
    private String value;

    // Constructor
    public AddValues (int id, String value){
        this.id = id;
        this.value= value;
   }

   // Parcelling part
   public AddValues (Parcel in){
       this.id = in.readInt();
       this.value= in.readString();
   }

   @Override
    public int describeContents() {
        return 0;
    }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
       dest.writeInt(id);
       dest.writeString(value);
   }

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

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

}

现在将列表添加到 Intent 额外内容应该很简单

额外添加列表

ArrayList<AddValue> list = new ArrayList<>();
Intent intent = new Intent(BluetoothLeService.this,HomePageFragment.class);
intent.putExtra("arg_key", list);

获取额外列表

ArrayList<AddValues> list = (ArrayList<AddValues>) intent.getSerializableExtra("arg_key");

顺便说一句,您可以使用Pair&lt;Integer, String&gt; 对象而不是创建AddValues 对象。这不会影响答案,但可能很高兴知道。

【讨论】:

  • 感谢 Kevin。当我通过 intent.putExtra 方法时,它给出了错误 Error:(220, 74) error: incompatible types: List cannot be convert to ArrayList
  • 没有。我会更新我的答案,在其中发现 2 个问题。 List 不能使用,因为它不是 Serializable 并且 Parcelable 类中有一些细微的变化。
  • 好的,等待回复。谢谢=)
  • 错误仍然存​​在。并且错误在 PutList 中。同样的错误。
  • @SantuReddy 我只是再次运行它而没有进行任何更改。它打印成功。确保您的代码是相同的,如果它仍然不起作用;尝试清理项目。
【解决方案2】:

您可以使用Intent#putExtra() 方法的putExtra("key", Serializable value) 变体在intent extra 中传递对象实例,您已经在此处使用了。

Intent intent = new Intent(BluetoothLeService.this,HomePageFragment.class);
intent.putExtra("id", data_id);
intent.putExtra("value", list);

现在要在其他 Activity 中获取这些数据,您需要使用 getSerializableExtra("key") 方法。

list = getIntent().getSerializableExtra("value");

但如果您想使用Parcelable,请参阅@kevins 回答。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 2013-04-12
    • 2016-11-08
    • 1970-01-01
    • 2020-07-08
    • 2016-09-06
    • 2020-05-21
    相关资源
    最近更新 更多