【问题标题】:Unable to update pojo class in Kotlin无法更新 Kotlin 中的 pojo 类
【发布时间】:2019-04-26 11:08:20
【问题描述】:

我正在尝试在 Kotlin 中的特定点击上更新我的 pojo 类,但它给了我错误:-

java.lang.stackoverflowerror: 堆栈大小 8mb

这是我的 Pojo 课程

class NavDrawerItem(var icon_normal: Int,var icon_notified: Int, var title: String, var isShowNotify: Boolean){
    var title1: String = title
        //  get() = title                    // Calls the getter recursively
        set(value)
        { title1 = value }

    var image: Int = icon_normal
        // get() = image
        set(value)
        { image = value }

    var image_notified: Int = icon_notified
        // get() = image
        set(value)
        { image_notified = value }


    var notify: Boolean = isShowNotify
        set(value) {
            notify = value
        }
}

我正在通过 NavigationDrawer 的 Item click 更新我的 Pojo

override fun onItemClick(position: Int) {
        mDrawerLayout?.closeDrawer(this!!.containerView!!)
        position1 = position
        for (i in navDrawerItems.indices) {
            val item = navDrawerItems[i]
            item.notify=(if (i == position) true else false)
            navDrawerItems[i] = item
            mAdapter?.notifyDataSetChanged()
        }
    }

请帮帮我!!!!

【问题讨论】:

  • mAdapter?.notifyDataSetChanged()移到循环外。你为什么不使用data class 来做pojo。
  • 它不工作,请分享一些代码

标签: android kotlin pojo


【解决方案1】:

您的设置器创建了无限循环,这会导致 StackOverflowError 异常。

class NavDrawerItem(var icon_normal: Int,var icon_notified: Int, var title: String, var isShowNotify: Boolean){
    var title1: String = title
        //  get() = title                    // Calls the getter recursively
        set(value)
        { field = value }

    var image: Int = icon_normal
        // get() = image
        set(value)
        { field = value }

    var image_notified: Int = icon_notified
        // get() = image
        set(value)
        { field = value }


    var notify: Boolean = isShowNotify
        set(value) {
            field = value
        }
}

上面是设置字段,你的实现是递归设置值。

另外,正如 ADM 所提到的,最好将 notifyDataSetChanged 移到循环之外,而不是在每次迭代时更新。

【讨论】:

    【解决方案2】:

    将您的类修改为简单的data class

    data class NavDrawerItem(var icon_normal: Int,var icon_notified: Int, var title: String, var isShowNotify: Boolean)
    

    还有

    override fun onItemClick(position: Int) {
        mDrawerLayout?.closeDrawer(this!!.containerView!!)
        for (i in navDrawerItems.indices) {
            val item = navDrawerItems[i]
            item.notify=(i == position)
        }
        mAdapter?.notifyDataSetChanged()
    }
    

    【讨论】:

      【解决方案3】:

      始终建议使用 数据类 来定义 pojo。 因为数据类仅用于存储数据。
      它们在 kotlin 中提供了许多比普通类更独特的功能。
      例如,您不需要定义 setter 和 getter,它们会自动添加到您的数据类中。
      此外,您的数据类会自动覆盖一些有用的函数,如 equalshashCodetoString 等。

      定义数据类非常简单。

      data class Foo ( val title : String, val isHungry : Boolean ){
      
      }
      

      【讨论】:

        猜你喜欢
        • 2017-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多