Reputation: 2997
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
Upvotes: 0
Views: 8458
Reputation: 76466
the types are different. That would be like trying to pass a List<String>
to a List<Integer>
.
i.e. trying to put a list of "Hello", "World" into a list expecting 1,2,3
You can't force one type to be another just by reference in Kotlin or Java.
If we assume the Kotlin module Depends on the Java module.
Only create the BasketItem
in the Java module, then have that be the type of your list whether it is in Kotlin or Java.
var basketitems: ArrayList<BasketItem> = ArrayList()
List<BasketItem> basketItems = new ArrayList<>();
Upvotes: 3