在Kotlin中创建一个EditText的嵌套列表:

huangapple go评论52阅读模式
英文:

Making a list of lists of lists of EditText in Kotlin

问题

  1. Main list that will hold a few other lists, each for one person (the table is for recording arrow scores in archery):

    • 主列表将保存几个其他列表,每个列表代表一个人(该表用于记录射箭中的箭靶得分)。
  2. The secondary player lists will each hold several (let's say around 20) smaller lists:

    • 次要的玩家列表将分别包含若干(假设大约20个)较小的列表。
  3. 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>,但期望的是List<MutableList<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:

  1. Main list that will hold a few other lists, each for one person (the table is for recording arrow scores in archery)
  2. The secondary player lists will each hold several (let's say around 20) smaller lists
  3. These lists will contain a few EditText objects each.
var recordData = List(columns) { mutableListOf&lt;MutableList&lt;out Any&gt;&gt;() }

// create a row in the header table with the archer&#39;s names
val firstRow = TableRow(this)
var view1 = TextView(this)
view1.text = &quot;  &quot;
firstRow.addView(view1)

archers.forEach { i -&gt;
    var name = TextView(this)
    name.text = i.toString().uppercase()
    name.setTextColor(Color.parseColor(&quot;#E2A300&quot;))
    name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20F)
    firstRow.addView(name)
    var playerArray = List(rows) { mutableListOf&lt;EditText&gt;() }
    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&lt;MutableList&lt;MutableList&lt;EditText&gt;&gt;&gt; = mutableListOf()
var playerList: MutableList&lt;MutableList&lt;EditText&gt;&gt; = mutableListOf()
recordData.add(playerList)

which then contain the EditText objects itself:

var rowList: MutableList&lt;EditText&gt; = 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&lt;MutableList&lt;EditText&gt;&gt; = mutableListOf()
...
...
..
archers.forEach { i -&gt;
    var name = TextView(this)
    name.text = i.toString().uppercase()
    name.setTextColor(Color.parseColor(&quot;#E2A300&quot;))
    name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20F)
    firstRow.addView(name)
var playerArray: MutableList&lt;EditText&gt; = MutableList(rows) { EditText(this) } //edit
    recordData.add(playerArray)
}

huangapple
  • 本文由 发表于 2023年6月12日 16:33:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定