无法将 onItemSelectListener() 与带有一项的 Spinner 一起使用

发布于 2024-12-07 16:47:08 字数 1484 浏览 1 评论 0原文

所以我有一个微调器(这里是 spinner2),它是通过 SQLite 表中的 ArrayAdapter 填充的。在选择一个项目时,我希望将其

  • 从数据库中删除
  • ,从微调器中删除

下面的代码实际上有效。除非旋转器只有一项。当这种情况发生时 似乎 onItemSelected 根本没有被调用。

我得到以下 LogCat

10-01 22:30:55.895: WARN/InputManagerService(1143): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45a06028

哦,当两个项目填充微调器时, spinner.getcount() 显示两个项目,所以这不是系统认为微调器为空或类似情况的奇怪情况。

这是代码:

public class SpinnerItemSelectListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            if(parent == spinner2){
                if(autoselected){
                    autoselected=false;
                }
                else{
                    //uniqvalarray is the arraymade from pulling data from SQLite and populaitng array adapter
                    Integer i = uniquevalarray.get(pos);
                    deleteRow(i);//deletes the row from the database and repopulates the above array.
                    autoselected=true;//just a boolean to stop autoslecting in onCreate()
                    //makeAlert(i);initially wanted to make alert box.
                    loadSpinner2();//reloads the spinner with new data


                }

            }



        }

        public void onNothingSelected(AdapterView parent) {
            //TODO
        }
    }

So I have a spinner (spinner2 here) which is populated via an ArrayAdapter from an SQLite table. On selecting an item I want it

  • deleted from the DB
  • deleted from the spinner

The code below actually works. Except when the spinner has only one item. When that happens
it seems onItemSelected is not called at all.

I get the following LogCat

10-01 22:30:55.895: WARN/InputManagerService(1143): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45a06028

Oh and when two items are populating the spinner, spinner.getcount() shows two items, so it's not some strange case of the system thinking the spinner is empty or something like that.

This is the code:

public class SpinnerItemSelectListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            if(parent == spinner2){
                if(autoselected){
                    autoselected=false;
                }
                else{
                    //uniqvalarray is the arraymade from pulling data from SQLite and populaitng array adapter
                    Integer i = uniquevalarray.get(pos);
                    deleteRow(i);//deletes the row from the database and repopulates the above array.
                    autoselected=true;//just a boolean to stop autoslecting in onCreate()
                    //makeAlert(i);initially wanted to make alert box.
                    loadSpinner2();//reloads the spinner with new data


                }

            }



        }

        public void onNothingSelected(AdapterView parent) {
            //TODO
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2024-12-14 16:47:08

微调器以这种方式运行:仅当您更改所选项目时才触发。如果您不更改该元素,因为它是唯一存在的元素,因此它无法更改。

我认为您必须使用的解决方案是使用微调器旁边的按钮来抛出删除功能。

您一定认为微调器不是为具有唯一元素而设计的,通常在您更改时才会导致更改所选择的一个。那么自然的解决方案就是这样。

The spinner runs this way : Only fire when you change the selected item . If you dont change that element , cause its the only one that exist , it can't change .

The solution i think you must use is using a button next to the spinner to throw the delete funcions.

You must think that Spinner is not made to be with an unique element , cause only changes usually when you change the selected one . then the natural solution can be that .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文