确定表单上项目的索引 (J2ME)
给定一个已附加到 Form
的 Item
,找出该项目在 Form 上的索引的最佳方法是什么?
Form.append(Item)
将为我提供最初添加的索引,但如果我稍后在此之前插入项目,索引将不同步。
Given an Item
that has been appended to a Form
, whats the best way to find out what index that item is at on the Form?
Form.append(Item)
will give me the index its initially added at, but if I later insert items before that the index will be out of sync.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我能想到的最好的:
我还没有真正测试过这个,但它应该可以工作,我只是不喜欢必须枚举每个项目,但永远不应该有那么多,所以我想它可以。
This was the best I could come up with:
I haven't actually tested this but it should work, I just don't like having to enumerate every item but then there should never be that many so I guess its ok.
好吧,只有两种方法可以做到这一点,因为 API 没有
indexOf(Item)
方法:Item
时更新获得的索引。 因此,当您在其他项目之前插入另一个Item
时,您必须更新这些项目的索引。 你可以为此保留某种阴影阵列,但这似乎有点矫枉过正。Form
的size
和get
方法遍历表单的所有项目。Well, there are just two ways to do this, since the API does not have an
indexOf(Item)
method:Item
. So when you insert anotherItem
before other items, you'll have to update the indices of those items. You could keep some kind of shadow-array for this, but that seems a bit overkill.size
andget
methods ofForm
.