【问题标题】:Unable to add elements to array of array that are mutable lists in koltin无法将元素添加到 kotlin 中可变列表的数组数组
【发布时间】:2021-10-17 04:54:02
【问题描述】:

我正在尝试将元素的可变列表数组添加到另一个数组数组的可变列表中。在这里,我在locationList 中有值作为 LatLng 对的可变列表,如下所示([lat/lng:(some value,some value),lat/lng:(some value,some value),lat/lng:(some value,一些价值)])。每次调用函数时,我都试图将列表添加到locationlists4。但我只在数组locationlists4[location4.size-1] 的最后一个列表中获取元素(像这样 [[],[],[lat/lng:(some value,some value),lat/lng:(some value,some value) ,lat/lng:(一些值,一些值)])。所有其他数组列表都是空的。


   var locationList = mutableListOf<LatLng>()

    var locationlists4 =  mutableListOf<MutableList<LatLng>>()




        private fun onPauseButtonClicked(){


        locationlists4.add(locationlist)
}


    private fun updateLocationList(location: Location) {
        val newLatLng = LatLng(location.latitude, location.longitude)
        locationList.value?.apply {
            add(newLatLng)
            locationList.postValue(this)
        }
    }

【问题讨论】:

  • 显示您在locationList 中添加纬度的代码。
  • 我已添加代码
  • locationList 对我来说看起来像 LiveData,而不是可变列表。你确定这是正确的代码吗?
  • 我做对了。看来我一直都是对的。每次调用该函数时,我都必须清除 locationList。这就是locationList 没有添加到locationlists4 的原因

标签: android arrays android-studio google-maps kotlin


【解决方案1】:

试试

    var locationList = mutableListOf<LatLng>()

    var locationlists4 =  mutableListOf<LatLng>()

    private fun onPauseButtonClicked() {
        locationlists4.addAll(locationlist)
    }

查看here 了解更多信息。

【讨论】:

  • 仍未添加。我试过这段代码locationlists4.addAll(listOf(locationList))
猜你喜欢
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 2014-05-06
  • 2012-09-11
  • 1970-01-01
  • 2015-05-11
相关资源
最近更新 更多