如何从选择一个项目后,多次发射过有人停止onItemSelected()?(How to stop

2019-10-19 23:10发布

我已经看到了另一个类似的线程,但我没能解决我的问题给定答案。

说明我的目标:

我有4个纺纱厂,每个人都有自己通过一个适配器分配给它的字符串的ArrayList的。 所有这些阵列都包含在开始时相同的值。

我想删除从所有其他纺纱选定的值(例如,在spinner1“项目”)(得自spinner2,3删除“项目”和4)当选择它。

问题:

当我选择来自不同纺纱第一两次或三次的项目( 再现问题所需的选择的数目而变化 )的onItemSelected()方法被调用多次(召唤的数目大于实际-user的数目大 - 选择制造)。

题:

如何防止调用onItemSelected(); 在不必要的时间。 我想它,只有当实际用户进行选择在纺纱厂之一被调用,只调用一次时确实会发生。

如果您想尝试帮助我,你需要更多的代码/设备本身问题的图像,请说出来。

这里是我的整个onItemSelected()方法:

@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(); ,纠正所有纺纱厂的选择,因为它不能正常工作,否则 - 它看起来像这样:

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.

*/

Answer 1:

为了防止onItemSelected()被调用,当你设置了微调,你可以做这样的:

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

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

当你调用notifyDatasetChanged框架进行各种检查,看看以前的选择,可以发现,所以onItemSelected可能会或可能不会被当微调器布局调用。

通过调用为setSelection(0,假)这些位置被设置为0,有可能检测到变化,但由于onItemSelectedListener为空,onItemSelected不会被解雇。 位置0被选中,因为我猜“默认---”值在列表中的第一个位置。 如果你喜欢,你可以选择其他位置。

当微调以后布局没有变化,所以onItemSelected不会在这里无论是开除。

请注意 ,这已经通过在API级别19中(Android 4.4 KitKat)的调试成立。 我不知道这是否适用于其他版本,我一直没能找到文档中的任何支持。



Answer 2:

您可以从之前通过在为setSelection方法可选动画领域作出选择用户射击停止微调。 一定要这种方式订购您的代码:

    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, yourList);
    spinner.setAdapter(spinnerAdapter);    
    spinner.setSelection(0, false);  //stops spinner from firing prior to user selection


Answer 3:

通过解释user1801374 ,我做了修复我的情况。 只要确保前后选择指数仍然为了不再次调用onItemSelected相同。

private int spinnerIndex = 0; 
spinner.setSelection(spinnerIndex, false);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 

    { public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                // Your code here 
               //I was detaching and reattaching one fragment which was calling the onItemSelected multiple times, recursively.
                spinnerIndex = i;
                spinner.setSelection(spinnerIndex, false);
                return;
            }

            public void onNothingSelected(AdapterView<?> adapterView) {
                // Your code here
                return;
            }
}


文章来源: How to stop onItemSelected() from firing off multiple times after a selection of an item was made?