不期望的onItemSelected电话 [英] Undesired onItemSelected calls

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

问题描述

我有36纺纱,我与一些值已初始化。我已经使用onItemSelectedListener他们。像往常一样,用户可以使用这些微调互动,烧制onItemSeected功能。

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.

一个问题是,调用初始化期间,但我发现的解决方案,在这里,并避免了使用全局变量计数,并检查是否计数> 36之前执行内部onItemSelected code。

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.

我的问题是这样的: 该用户可以选择点击一个名为previous按钮,在我需要重置一些微调值。

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.

我试图改变计数的值设置为0重置纺纱,然后重置后更改回37之前,但我已经认识到了onItemSelected被称为只有在所有其他功能执行完毕,所以它是所谓计数后改回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.

推荐答案

我发现了一个简单的,我认为,优雅的解决方案。 使用标签。 我首先创建了一个名为标签,并把下面的code新的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,我执行code 如果(spin.getTag(R.id.pos)!=位置),其中位置是由功能提供的位置的变量。 以这种方式,我的code为执行仅当用户进行选择。 由于用户已经做出了选择,标签一直没有更新,所以处理完成后,我更新标记为 spin.setTag(R.id.pos,位置)

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天全站免登陆