0

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
2
  • the types are different. That would be like trying to pass a List<String> to a List<Integer> Commented Aug 30, 2019 at 10:50
  • checkout the imports and if var basketitems: ArrayList<BasketItem> = ArrayList() is using correct BasketItem as you have two basket items Commented Aug 30, 2019 at 10:54

1 Answer 1

3

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<>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.