英文:
Making a list of lists of lists of EditText in Kotlin
问题
-
Main list that will hold a few other lists, each for one person (the table is for recording arrow scores in archery):
- 主列表将保存几个其他列表,每个列表代表一个人(该表用于记录射箭中的箭靶得分)。
-
The secondary player lists will each hold several (let's say around 20) smaller lists:
- 次要的玩家列表将分别包含若干(假设大约20个)较小的列表。
-
These lists will contain a few EditText objects each:
- 这些列表将包含每个几个EditText对象。
var recordData = List(columns) { mutableListOf<MutableList<out Any>>() }
// create a row in the header table with the archer's names
val firstRow = TableRow(this)
var view1 = TextView(this)
view1.text = " "
firstRow.addView(view1)
archers.forEach { i ->
var name = TextView(this)
name.text = i.toString().uppercase()
name.setTextColor(Color.parseColor("#E2A300"))
name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20F)
firstRow.addView(name)
var playerArray = List(rows) { mutableListOf<EditText>() }
recordData += playerArray
}
这是你生成表格并尝试初始化前两个列表的Kotlin代码。然而,最后一行出现了以下错误:
错误类型不匹配:推断类型是List<MutableList
我曾尝试使用数组进行这项操作,但基本上遇到了相同的问题 - 如何初始化一个包含EditText对象的列表/数组的列表/数组?我已经卡在这个问题上几个小时了,所以我会非常感谢任何建议。
英文:
I am fairly new to Kotlin and am trying to develop an app which requires me to access all EditText data from a table, that I generate programmatically also with Kotlin. That I managed to do, but I have been unsuccessful in creating lists that will hold all those instances of EditText. To sum it up, I need:
- Main list that will hold a few other lists, each for one person (the table is for recording arrow scores in archery)
- The secondary player lists will each hold several (let's say around 20) smaller lists
- These lists will contain a few EditText objects each.
var recordData = List(columns) { mutableListOf<MutableList<out Any>>() }
// create a row in the header table with the archer's names
val firstRow = TableRow(this)
var view1 = TextView(this)
view1.text = " "
firstRow.addView(view1)
archers.forEach { i ->
var name = TextView(this)
name.text = i.toString().uppercase()
name.setTextColor(Color.parseColor("#E2A300"))
name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20F)
firstRow.addView(name)
var playerArray = List(rows) { mutableListOf<EditText>() }
recordData += playerArray
}
This is the kotlin code where I generate the table and try to initialize the first two lists. However, the last line creates this error:
Type mismatch: inferred type is List<MutableList<out Any>> but List<MutableList<MutableList<out Any>>> was expected
I tried doing this with also with Arrays, but hit essentially the same problem - how do I initialize a list/array of lists/arrays of lists/arrays of EditText? I have been stuck at this for a couple of hours so I'd be very grateful for any advice.
答案1
得分: 1
我的最终解决方案是创建一个包含多个其他列表的列表:
val recordData: MutableList<MutableList<MutableList<EditText>>> = mutableListOf()
var playerList: MutableList<MutableList<EditText>> = mutableListOf()
recordData.add(playerList)
然后,这些列表包含EditText对象本身:
var rowList: MutableList<EditText> = MutableList(rows) { EditText(this) }
var tv = EditText(this)
rowList.add(tv)
然后,这些EditText对象被添加回那些playerList列表中:
recordData[col-1].add(rowList)
英文:
My final solution for making a list of lists of lists of EditText:
main list contains several other lists
val recordData: MutableList<MutableList<MutableList<EditText>>> = mutableListOf()
var playerList: MutableList<MutableList<EditText>> = mutableListOf()
recordData.add(playerList)
which then contain the EditText objects itself:
var rowList: MutableList<EditText> = MutableList(rows) {EditText(this)}
var tv = EditText(this)
rowList.add(tv)
And these are then added back to those playerList lists:
recordData[col-1].add(rowList)
答案2
得分: 0
尝试这个:
val recordData: MutableList<MutableList<EditText>> = mutableListOf()
...
...
..
archers.forEach { i ->
var name = TextView(this)
name.text = i.toString().uppercase()
name.setTextColor(Color.parseColor("#E2A300"))
name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20F)
firstRow.addView(name)
var playerArray: MutableList<EditText> = MutableList(rows) { EditText(this) } //edit
recordData.add(playerArray)
}
英文:
Try this:
val recordData: MutableList<MutableList<EditText>> = mutableListOf()
...
...
..
archers.forEach { i ->
var name = TextView(this)
name.text = i.toString().uppercase()
name.setTextColor(Color.parseColor("#E2A300"))
name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20F)
firstRow.addView(name)
var playerArray: MutableList<EditText> = MutableList(rows) { EditText(this) } //edit
recordData.add(playerArray)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论