在选择一个项目之后,如何阻止onItemSelected()多次触发? [英] How to stop onItemSelected() from firing off multiple times after a selection of an item was made?

查看:81
本文介绍了在选择一个项目之后,如何阻止onItemSelected()多次触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了另一个类似的主题,但是我无法通过给定的答案解决问题.

I've seen another similar thread, but I wasn't able to resolve my issue with the given answers.

我的目标的解释:

我有4个微调器,每个微调器都有自己的通过适配器分配给它的字符串ArrayList.所有这些数组的开头都包含相同的值.

I have 4 spinners, each has its own ArrayList of strings assigned to it via an adapter. All of these arrays contain the same values at the beginning.

我希望在选中它时从所有其他微调器中删除所选值(例如,微调器1中的"item")(从微调器2、3和4中删除"item").

I want to remove the selected value (eg. "item" in spinner1) from all the other spinners (remove "item" from spinner2, 3 and 4) when it is selected.

问题:

当我从不同的微调器中选择前两个或三个项目时(重现问题所需的选择次数有所不同),onItemSelected()方法被多次调用(调用次数大于实际的-user-选择次数).

When I select an item for the first two or three times from different spinners (the number of selections needed to reproduce the problem varies) the onItemSelected() method gets called multiple times (the number of callings is greater than the number of actual -user- selections made).

问题:

如何防止在不必要的时间调用onItemSelected();.我希望仅当实际用户在其中一个微调器中进行选择时才调用它,并且在确实发生这种情况时才调用它.

How to prevent the calling of onItemSelected(); at unnecessary times. I want it to be called only when the actual user makes a selection in one of the spinners and only call it once when that does happen.

如果您想尝试帮助我,并且需要设备本身上更多的问题代码/图像,请这样说.

If you want to try to help me out and you need more code / images of the problem on the device itself, please, say so.

这是我整个onItemSelected()方法:

Here is my whole onItemSelected() method:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if (inCorrection == false)
    {
        s1 = spinner1.getSelectedItemPosition();
        s2 = spinner2.getSelectedItemPosition();
        s3 = spinner3.getSelectedItemPosition();
        s4 = spinner4.getSelectedItemPosition();
        testcount++;
        switch(parent.getId())
        {
            case R.id.v1_q1_s1:
                    if((position != AdapterView.INVALID_POSITION) && (spinner1.getSelectedItem().toString() != "Default---"))
                    {
                        findLists(myList2, myList3, myList4, spinner1.getSelectedItem().toString());
                        if(returnChecks(0) != "Default---")
                        {
                            myList2.add(returnChecks(0)); 
                            myList3.add(returnChecks(0)); 
                            myList4.add(returnChecks(0)); 
                        }
                        addChecks(0, (spinner1.getSelectedItem().toString())); 
                    }
                    else
                    {
                        if(position != AdapterView.INVALID_POSITION)
                        {
                            myList2.add(returnChecks(0)); 
                            myList3.add(returnChecks(0)); 
                            myList4.add(returnChecks(0)); 
                            addChecks(0, (spinner1.getSelectedItem().toString())); 
                        }
                    }
                    adapter1.notifyDataSetChanged();
                    adapter2.notifyDataSetChanged();
                    adapter3.notifyDataSetChanged();
                    adapter4.notifyDataSetChanged();
                    Toast.makeText(Vprasalnik1.this, myList1.toString()+"\n"+myList2.toString()+"\n"+myList3.toString()+"\n"+myList4.toString()+"\n"+checks.toString(), Toast.LENGTH_LONG).show();
                break;
            case R.id.v1_q1_s2:
                if((position != AdapterView.INVALID_POSITION) && (spinner2.getSelectedItem().toString() != "Default---"))
                {
                    findLists(myList1, myList3, myList4, spinner2.getSelectedItem().toString());
                    if(returnChecks(1) != "Default---")
                    {
                        myList1.add(returnChecks(1)); 
                        myList3.add(returnChecks(1)); 
                        myList4.add(returnChecks(1)); 
                    }
                    addChecks(1, (spinner2.getSelectedItem().toString())); 
                }
                else
                {
                    if(position != AdapterView.INVALID_POSITION)
                    {
                        myList1.add(returnChecks(1)); 
                        myList3.add(returnChecks(1)); 
                        myList4.add(returnChecks(1)); 
                        addChecks(1, (spinner2.getSelectedItem().toString())); 
                    }
                }
                adapter1.notifyDataSetChanged();
                adapter2.notifyDataSetChanged();
                adapter3.notifyDataSetChanged();
                adapter4.notifyDataSetChanged();
                Toast.makeText(Vprasalnik1.this, myList1.toString()+"\n"+myList2.toString()+"\n"+myList3.toString()+"\n"+myList4.toString()+"\n"+checks.toString(), Toast.LENGTH_LONG).show();
                break;
            case R.id.v1_q1_s3:
                if((position != AdapterView.INVALID_POSITION) && (spinner3.getSelectedItem().toString() != "Default---"))
                {
                    findLists(myList2, myList1, myList4, spinner3.getSelectedItem().toString());
                    if(returnChecks(2) != "Default---")
                    {
                        myList2.add(returnChecks(2)); 
                        myList1.add(returnChecks(2)); 
                        myList4.add(returnChecks(2)); 
                        Toast.makeText(Vprasalnik1.this, "before: "+returnChecks(2), Toast.LENGTH_LONG).show();
                    }
                    addChecks(2, (spinner3.getSelectedItem().toString())); 
                    Toast.makeText(Vprasalnik1.this, "after: "+returnChecks(2), Toast.LENGTH_LONG).show();
                }
                else
                {
                    if(position != AdapterView.INVALID_POSITION)
                    {
                        myList2.add(returnChecks(2)); 
                        myList1.add(returnChecks(2)); 
                        myList4.add(returnChecks(2)); 
                        addChecks(2, (spinner3.getSelectedItem().toString())); 
                    }
                }
                adapter1.notifyDataSetChanged();
                adapter2.notifyDataSetChanged();
                adapter3.notifyDataSetChanged();
                adapter4.notifyDataSetChanged();
                Toast.makeText(Vprasalnik1.this, myList1.toString()+"\n"+myList2.toString()+"\n"+myList3.toString()+"\n"+myList4.toString()+"\n"+checks.toString(), Toast.LENGTH_LONG).show();
                break;
            case R.id.v1_q1_s4:
                if((position != AdapterView.INVALID_POSITION) && (spinner4.getSelectedItem().toString() != "Default---"))
                {
                    findLists(myList2, myList3, myList1, spinner4.getSelectedItem().toString());
                    if(returnChecks(3) != "Default---")
                    {
                        myList2.add(returnChecks(3)); 
                        myList3.add(returnChecks(3)); 
                        myList1.add(returnChecks(3)); 
                    }
                    addChecks(3, (spinner4.getSelectedItem().toString())); 
                }
                else
                {
                    if(position != AdapterView.INVALID_POSITION)
                    {
                        myList2.add(returnChecks(3)); 
                        myList3.add(returnChecks(3)); 
                        myList1.add(returnChecks(3)); 
                        addChecks(3, (spinner4.getSelectedItem().toString())); 
                    }
                }
                adapter1.notifyDataSetChanged();
                adapter2.notifyDataSetChanged();
                adapter3.notifyDataSetChanged();
                adapter4.notifyDataSetChanged();
                Toast.makeText(Vprasalnik1.this, myList1.toString()+"\n"+myList2.toString()+"\n"+myList3.toString()+"\n"+myList4.toString()+"\n"+checks.toString(), Toast.LENGTH_LONG).show();
                break;
        }
        correctSelection();
    }
}

在上述代码的结尾处,有一个我称为correctSelection();的函数,该函数可以纠正所有微调器的选择,因为否则它无法正常工作-看起来像这样:

At the end of the above code there is a function I call named correctSelection();, that corrects the selection of all spinners, because it doesn't work correctly otherwise - it looks like this:

void correctSelection()
{
    inCorrection = true;
    spinner1.setSelection(myList1.lastIndexOf(returnChecks(0)));
    spinner2.setSelection(myList2.lastIndexOf(returnChecks(1)));
    spinner3.setSelection(myList3.lastIndexOf(returnChecks(2)));
    spinner4.setSelection(myList4.lastIndexOf(returnChecks(3)));
    inCorrection = false;
}
/*it sets the position of all spinners to the last "saved"
(current) item selected, so it corrects the possible index offset that occurs otherwise
(returnChecks(); returns the last item selected from an array in a string format)

PS: To avoid the calling of onItemSelected() in case of programmatically setting the selection
of spinners, I've input a boolean flag (variable "inCorrection"), which is set to false before the
selections are made by "the application" and then set back to false when the code gets run.

*/

推荐答案

要防止在设置微调器时调用onItemSelected(),可以这样做:

To prevent onItemSelected() from being called when you set up the spinner, you can do it like this:

spinner.setOnItemSelectedListener(null);
adapter.notifyDatasetChanged();
spinner.setSelection(0, false);
spinner.setOnItemSelectedListener(onItemSelectedListener);

说明:
该框架时触发发生在选择的变化onItemSelected事件.它通过注册当前选定位置和上一个选定位置(mSelectedPostion和mOldSelectedPosition)来检测更改.

Explanation:
The framework fires the onItemSelected event when a change in the selection has occurred. It detects a change by registering the current selected position and the previous selected position (mSelectedPostion and mOldSelectedPosition).

当您调用notifyDatasetChanged时,框架会执行各种检查以查看是否可以找到先前的选择,因此在放置微调器时可能会调用onItemSelected,也可能不会调用.

When you call notifyDatasetChanged the framework performs various checks to see if the previous selection can be found, so onItemSelected may or may not be called when the spinner is laid out.

通过调用setSelection(0,false)将这些位置设置为0,可能检测到更改,但是由于onItemSelectedListener为null,因此不会触发onItemSelected.选择位置0是因为我想默认---"值是列表中的第一个位置.您可以根据需要选择其他职位.

By calling setSelection(0, false) these positions are set to 0, possibly detecting a change, but since onItemSelectedListener is null, onItemSelected wont be fired. Position 0 is selected because I guess the "Default---" value is the first position in the list. You can choose another position if you like.

稍后放置微调器时,没有任何变化,因此也不会在此触发onItemSelected.

When the spinner is later laid out there is no change, so onItemSelected wont be fired here either.

注意,这是通过在API级别19(Android 4.4 KitKat)上进行调试而建立的.我不知道它是否可以在其他版本上使用,并且我也无法在文档中找到任何支持它的东西.

Note that this has been established by debugging on API level 19 (Android 4.4 KitKat). I don't know if it works on other versions, and I haven't been able to find anything in the documentation to support it.

这篇关于在选择一个项目之后,如何阻止onItemSelected()多次触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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