目前正在开发一个应用程序,我决定用 Kotlin 编写。但是,应用程序与最初用 Java 编写的单独模块进行交互。
我有以下 Kotlin 数据类:
data class BasketItem(
@SerializedName("id") var id :String ,
@SerializedName("parentID") var parentID: String ,
@SerializedName("barcode") var barcode : String ,
@SerializedName("guid") var guid : String ,
@SerializedName("functionalName") var functionalName : String ,
@SerializedName("posPosition") var posPosition : Int ,
@SerializedName("itemvalue") var itemvalue : ItemValue ,
@SerializedName("quantity") var quantity :Int )
{
constructor(): this("","","","","",0,ItemValue(),0)
}
data class ItemValue(
@SerializedName("costpriceexcl") var costpriceexcl: Double ,
@SerializedName("costpriceincl") var costpriceincl :Double ,
@SerializedName("sellingpriceExc") var sellingpriceExc : Double ,
@SerializedName("sellingpriceIncl") var sellingpriceIncl : Double ,
@SerializedName("vatAmount") var vatAmount : Double )
{
constructor():this (0.0,0.0,0.0,0.0,0.0)
}
var basketitems: ArrayList<BasketItem> = ArrayList()
我正在尝试将此 ArrayList 传递给用 java 编写的模块。我创建了具有相同参数的等效类
缩短的 java 等效类。我没有包括构造函数、getter 和 setter。
public class BasketItem
{
public String id;
public String parentID;
public String barcode;
public String guid;
public String functionalName;
public Integer posPosition;
public ItemValue itemvalue ;
public Integer quantity ;
}
public class ItemValue
{
private Double costpriceexcl;
private Double costpriceincl;
private Double sellingpriceExc;
private Double sellingpriceIncl;
private Double vatAmount;
public ItemValue()
{
}
public ItemValue(Double costpriceexcl, Double costpriceincl, Double sellingpriceExc, Double sellingpriceIncl, Double vatAmount)
{
this.costpriceexcl = costpriceexcl;
this.costpriceincl = costpriceincl;
this.sellingpriceExc = sellingpriceExc;
this.sellingpriceIncl = sellingpriceIncl;
this.vatAmount = vatAmount;
}
}
当我尝试将 Arraylist 从 Kotlin 端传递到 java 端时。我收到以下错误:
类型不匹配:推断的类型是
kotlin.collections.ArrayList<com.rewards.Model.Models.BasketItem>
/* = java.util.ArrayList<com.rewards.Model.Models.BasketItem> */
but java.util.ArrayList<com.data.entities.POS.BasketItem> was expected
类型不同。这就像试图将 a 传递List<String>
给 a List<Integer>
。
即试图将“Hello”、“World”列表放入期望 1,2,3 的列表中
您不能仅通过 Kotlin 或 Java 中的引用来强制一种类型成为另一种类型。
如果我们假设 Kotlin 模块依赖于 Java 模块。
只BasketItem
在 Java 模块中创建,然后让它成为您列表的类型,无论是在 Kotlin 还是 Java 中。
var basketitems: ArrayList<BasketItem> = ArrayList()
List<BasketItem> basketItems = new ArrayList<>();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句