不需要的 onItemSelected 调用 [英] Undesired onItemSelected calls

查看:46
本文介绍了不需要的 onItemSelected 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 36 个微调器,它们已经用一些值进行了初始化.我和他们一起使用了 onItemSelectedListener .像往常一样,用户可以与这些微调器进行交互,触发 onItemSecected 函数.

I have 36 spinners that I have initialized with some values. I have used onItemSelectedListener with them. As usual, the user can interact with these spinners, firing the onItemSeected function.

一个问题是调用是在 init 期间进行的,但我在这里找到了解决方案,并避免使用全局变量count"并在执行 onItemSelected 中的代码之前检查 count > 36.

One problem is that the call is made during init, but I found solutions to it here and avoided that using a global variable "count" and checking if count > 36 before executing code inside onItemSelected.

我的问题是:用户可以选择单击一个名为上一个"的按钮,我必须在该按钮上重置一些微调器值.

My problem is this: The user has the option to click on a button called "Previous", upon which I have to reset SOME of the spinner values.

我尝试在重置微调器之前将 count 的值更改为 0,然后在重置后将其更改回 37,但我已经明白 onItemSelected 仅在所有其他函数执行完成后才被调用,所以它是即使微调值在用户选择后立即设置,调用 AFTER count 也会改回 37.

I tried changing the value of count to 0 before resetting the spinners, and then changing it back to 37 after resetting, but I have come to understand that the onItemSelected is called only after every other function is done executing, so it is called AFTER count is changed back to 37 even though the spinner values are set as soon as they are selected by user.

我需要在不触发 onItemSelected 函数的情况下反复刷新一些微调器.任何人都可以帮我找到解决方案吗?谢谢.

I need to repeatedly refresh some spinners WITHOUT firing off the onItemSelected function. Can anyone please help me find a solution? Thanks.

推荐答案

我找到了一个简单而优雅的解决方案.使用标签.我首先创建了一个名为tags"的新 XML 文件并输入以下代码:

I found a simple and, I think, elegant solution. Using tags. I first created a new XML file called 'tags' and put in the following code:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
  <item name="pos" type="id" />
</resources>

每当我自己使用 spin.setSelection(pos) 时,我也会使用 spin.setTag(R.id.pos, pos),所以我正在设置当前位置作为标签.

Whenever I myself use spin.setSelection(pos), I also do spin.setTag(R.id.pos, pos), so I am setting the current position as a tag.

然后,在 onItemSelected 中,我只执行代码 if(spin.getTag(R.id.pos) != position),其中 position 是函数提供的位置变量.这样,我的代码只有在用户进行选择时才会执行.由于用户进行了选择,标签没有更新,所以处理完成后,我将标签更新为spin.setTag(R.id.pos, position).

Then, in onItemSelected, I am executing code only if(spin.getTag(R.id.pos) != position), where position is the position variable supplied by the function. In this way, my code is executed only when the user is making a selection. Since the user has made a selection, the tag has not been updated, so after the processing is done, I update the tag as spin.setTag(R.id.pos, position).

注意:始终使用相同的适配器很重要,否则位置"变量可能指向不同的元素.

NOTE: It is important to use the same adapter throughout, or the "position" variable might point to different elements.

正如 kaciula 所指出的,如果您不使用多个标签,则可以使用更简单的版本,即 spin.setTag(pos)spin.getTag() 无需 XML 文件.

As kaciula pointed out, if you're not using multiple tags, you can use the simpler version, that is spin.setTag(pos) and spin.getTag() WITHOUT the need for an XML file.

这篇关于不需要的 onItemSelected 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆