java - 将模型类加载到 Kotlin 类

标签 java android kotlin

我有一个 Model 类的 java 实现,正在加载到 Java 类中。我将类转换为 Kotlin,但现在遇到 Unresolved reference 负载

我尝试过 URlclassloader,但这会产生更多错误

recyclerview中onBindViewHolder的Java实现


    holder.decreaseButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(AppDatastore.getPosData().get(position).getProductItemQuantity().equals("1")){

                        }else {

                            int count = 1;
                            count = Integer.parseInt(AppDatastore.getPosData().get(position).getProductItemQuantity());
                            long id = AppDatastore.getPosData().get(position).getId();
                            int sum = count - 1;
                            PosData posData = PosData.load(PosData.class, id);
                            posData.setProductItemQuantity(sum + "");
                            posData.save();
                        }

                        holder.integerNumber.setText(AppDatastore.getPosData().get(position).getProductItemQuantity());
                        ipassPos.passPos();

                    }
                });

Kotlin 类

 holder.decreaseButton.setOnClickListener {
            if (AppDatastore.posData[position].productItemQuantity == "1") {

            } else {

                var count = 1
                count = Integer.parseInt(AppDatastore.posData[position].productItemQuantity)
                val id = AppDatastore.posData[position].id!!
                val sum = count - 1

          //error occurs here showing unresolved reference load

                val posData = PosData.load<PosData>(PosData::class.java, id)
                posData.productItemQuantity = sum.toString() + ""
                posData.save()
            }

            holder.integerNumber.setText(AppDatastore.posData[position].productItemQuantity)
            ipassPos.passPos()
        }

PosData 类是

@Table(name = "PosData")
class PosData : Model {


    @Column(name = "productItemId")
    var productItemId: String? = null
    @Column(name = "productItemName")
    var productItemName: String? = null
    @Column(name = "productItemQuantity")
    var productItemQuantity: String? = null
    @Column(name = "productItemKind")
    var productItemKind: String? = null
    @Column(name = "productItemUnitPrice")
    var productItemUnitPrice: String? = null
    @Column(name = "productItemImage")
    var productItemImage: String? = null

    constructor() : super() {}

    constructor(productItemId: String, productItemName: String, productItemQuantity: String, productItemKind: String, productItemUnitPrice: String, productItemImage: String) {
        this.productItemId = productItemId
        this.productItemName = productItemName
        this.productItemQuantity = productItemQuantity
        this.productItemKind = productItemKind
        this.productItemUnitPrice = productItemUnitPrice
        this.productItemImage = productItemImage
    }

    fun setPosData(productItemId: String, productItemName: String, productItemQuantity: String, productItemKind: String, productItemUnitPrice: String, productItemImage: String) {
        this.productItemId = productItemId
        this.productItemName = productItemName
        this.productItemQuantity = productItemQuantity
        this.productItemKind = productItemKind
        this.productItemUnitPrice = productItemUnitPrice
        this.productItemImage = productItemImage
    }

    fun posDataList(): List<PosData> {
        return getMany(PosData::class.java, "PosData")
    }
}

我有没有办法在 Kotlin 中做到这一点,或者甚至更好的方法会有很大帮助

最佳答案

试试这个。我正在使用下面的类模型:-

class AddAlertOverViewItem {

    @Expose
    @SerializedName("email")
    lateinit var email: String
    @Expose
    @SerializedName("alert_type")
    lateinit var alertType: String
    @Expose
    @SerializedName("alert_name")
    lateinit var alertName: String
    @Expose
    @SerializedName("sms")
    lateinit var sms: String
    @Expose
    @SerializedName("alert_id")
    lateinit var alertId: String
    @Expose
    @SerializedName("notification")
    lateinit var notification: String
}

在您的适配器类中。像这样访问。

addAlertOverViewItem.sms
addAlertOverViewItem.alertType
addAlertOverViewItem.notification
addAlertOverViewItem.alertName

这里 addAlertOverViewItem 是模型类的对象,sms、alertType 等是模型 key ,用于访问该 key 的值。

关于java - 将模型类加载到 Kotlin 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55948280/

相关文章:

java - ECDSA KeyPairGenerator 不可用(但在 JUnit 中有效)

java - JSF持久性文件问题

java - 为什么将单个项目字符串解析为 JSONArray 失败

kotlin - 在一个类轮中,对 listOf() 为 null,对 Kotlin 中的 listOf(value) 不为 null?

kotlin - kotlin 中的字段注入(inject)始终为空

java - Tomcat 找不到 Spring Controller

java - 在 equals() 方法中比较 double 的正确方法

使用 adb 命令捕获时,Android 4.4 不允许我保存图片

android - qt-creator android部署错误

android - 为什么 AsyncPagingDataDiffer submitData() 应该卡住和超时测试?