如何禁用onItemSelectedListener设定选定的项目由code时要调用时要、项目、onItemSelectedListener、code

2023-09-06 01:41:42 作者:顾念i

只是想知道你是如何处理以下问题:取决于两个微调所选项目的结果被计算出来。为了处理用户界面的东西,即用户选择在纺纱中的一个新的项目,我安装使用监听器 setOnItemSelectedListener 在微调我的的onCreate ()活动的方法。

Just wondering how you handle the following problem: a result is calculated depending on two spinners' selected items. To handle the UI things, i.e. a user picks a new item in one of the spinners, I install a listener using setOnItemSelectedListener for the spinner in my onCreate() method of the activity.

现在:该作品,当然,罚款。听者的工作是触发结果的一个新的计算。

Now: that works, of course, fine. The listener's work is to trigger a new calculation of the result.

问题:因为我拦截的onPause() onResume()保存/恢复过去的状态,我有一个方法,它的设置这两个微调选定的编程就像在这里的项目:

The problem: because I intercept onPause() onResume() to save/restore the last state, I got a method that sets these two spinners' selected item programmatically like in here:

startSpinner.setSelection(pStart);
destSpinner.setSelection(pDest);

这两个电话调用监听器呢!被调用过两次我的计算方法,结果加一个新的结果集的通知!

These two calls invoke the listeners, too! My calculation method for the result plus the notification of a new result set is invoked twice here!

一个愚蠢直接的办法了,这将是有一个布尔变量的禁用无论听众做内部,设置选定的项目,之后重置之前设置它。 好。但是,有没有更好的方法?

A stupid direct approach for this would be to have a boolean variable disabling whatever the listener does inside, setting it before setting the selected items and resetting it afterwards. Okay. But is there a better method??

我不希望听众通过code被称为 - 行动,只能由用户操作! : - (

I don't want listeners to be called by code - actions, only by user actions! :-(

你怎么办呢? 谢谢!

推荐答案

我有一个更简单的,而且我认为,更好的解决方案。因为我甚初始化后刷新纺纱厂,这是一个比较通用的方法。 详情请参阅接受的答案:

I have an easier, and I think, better solution. Since I had to refresh the spinners even after initialization, this is a more generic approach. Please refer the accepted answer:

Undesired onItemSelected电话