Android 无法更新Kotlin中的pojo类
我试图在Kotlin中的某个特定单击上更新我的pojo类,但它给了我错误:- java.lang.StackOverflower错误:堆栈大小为8mb 这是我的Pojo课程Android 无法更新Kotlin中的pojo类,android,kotlin,pojo,Android,Kotlin,Pojo,我试图在Kotlin中的某个特定单击上更新我的pojo类,但它给了我错误:- java.lang.StackOverflower错误:堆栈大小为8mb 这是我的Pojo课程 class NavDrawerItem(var icon_normal: Int,var icon_notified: Int, var title: String, var isShowNotify: Boolean){ var title1: String = title // get() =
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项目的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()
}
}
请帮帮我 设置程序创建无限循环,这会导致
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移到循环之外,并且不要在每次迭代时更新。您的设置程序创建无限循环,这会导致
堆栈溢出错误
异常
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移到循环之外,并且不要在每次迭代时更新。将类修改为一个简单的类 及
将类修改为一个简单的类 及
始终建议使用数据类来定义POJO。 因为数据类只用于存储数据
与kotlin的普通课程相比,它们提供了许多独特的功能。
例如,您不需要定义setter和getter,它们会自动添加到数据类中。
此外,您的数据类将自动覆盖一些有用的函数,如equals、hashCode、toString等。
定义数据类非常简单。
data class Foo ( val title : String, val isHungry : Boolean ){
}
始终建议使用数据类来定义POJO。 因为数据类只用于存储数据
与kotlin的普通课程相比,它们提供了许多独特的功能。
例如,您不需要定义setter和getter,它们会自动添加到数据类中。
此外,您的数据类将自动覆盖一些有用的函数,如equals、hashCode、toString等。
定义数据类非常简单。
data class Foo ( val title : String, val isHungry : Boolean ){
}
移动
mAdapter?.notifyDataSetChanged()
外部循环。为什么不使用for pojo。它不起作用,请共享一些代码移动mAdapter?.notifyDataSetChanged()
外部循环。为什么不使用for pojo。它不起作用,请共享一些代码