如何使用 Kotlin 转换 Android Studio 中字符串列表中的 StringBuilder 项?

How can I convert a StringBuilder item in a List of strings in Android Studio using Kotlin?

我的改造中有这个 StringBuilder 项目:

 retrofitData.enqueue(object:Callback<List<HospitalResponse>?>{
            override fun onResponse(call: Call<List<HospitalResponse>?>, response: Response<List<HospitalResponse>?>) {
                val responseData = response.body()!!
                val myStringBuilder = StringBuilder()
                for(hospitalResponse in responseData){
                    myStringBuilder.append(hospitalResponse.hospitalName)
                    myStringBuilder.append("\n")
                }

我想将 StringBuilder 转换为列表:

val hospitals = mutableListOf<String>()

我该怎么做?

我看到三个选项:

  1. 获取 StringBuilder 并将其转换为列表(不推荐)
val list1 = myStringBuilder.toString().trim('\n').split("\n")
  1. 创建一个列表并将名称放入列表而不是将其附加到 StringBuilder
val list2 = ArrayList<String>()
for(hospitalResponse in responseData){
   list2.add(hospitalResponse.hospitalName)
}

  1. 使用折叠功能
val list3 = responseData.fold(ArrayList<String>()) { list, value ->
    list.add(value.hospitalName)
    list
}