Currently working on an application, which I have decided to write in Kotlin. However, the application interacts with separate modules that were initially written in Java.
I have the following Kotlin data classes:
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()
I am trying to pass this ArrayList along to a module that was written in java. I created equivalent classes with the same parameters
The shortened java equivalent Class. I have not included the constructor, getter and setters.
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;
}
}
When I try and pass the Arraylist from the Kotlin side to the java side. I get the following error:
Type mismatch: inferred type is
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
List<String>to aList<Integer>var basketitems: ArrayList<BasketItem> = ArrayList()is using correctBasketItemas you have two basket items