【问题标题】:Android spinner, nothing selectedAndroid 微调器,未选择任何内容
【发布时间】:2018-07-28 06:28:50
【问题描述】:

当我的微调器访问第一个案例并立即重定向时,我遇到了问题。如何使用:

@Override
    public void onNothingSelected(AdapterView<?> view); {
        // TODO Auto-generated method stub

    }

在用户做出选择之前正确停留在页面上的方法。下面是我的代码。

// Creating adapter for spinner & choosing Drop down layout style - list view
    ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.event,android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinner.setAdapter(adapter);

    //spinner needs to know who is responsible for handling events
    spinner.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {

    //casting the view to textView
    TextView myText=(TextView) view;

    // use .getText to display what text was selected by user
    Toast.makeText(this,"You Selected "+myText.getText(),Toast.LENGTH_SHORT).show();



    switch (pos) {
        case (0):
            //Case selection redirecting user to 'Training Table'
            Intent a = new Intent(Calendar.this, TrainingTable.class);
            Calendar.this.startActivity(a);
            break;
        case (1):
            //Case selection redirecting user to 'Race Table'
            Intent b = new Intent(Calendar.this, Races.class);
            Calendar.this.startActivity(b);
            break;

        case (2):
            //Case selection redirecting user to 'Workshops page'
            Intent c = new Intent(Calendar.this, Workshops.class);
            Calendar.this.startActivity(c);
            break;
    }

【问题讨论】:

    标签: android spinner selection


    【解决方案1】:

    强制微调器选择一个项目:

    spinner.setSelection(0);
    

    然后设置监听器:

    spinner.setOnItemSelectedListener(this);
    

    这只需要在创建微调器时执行一次。这样您就可以避免收到对 OnItemSelectedListener 的不必要调用,默认情况下调用一次。

    【讨论】:

    • 请注意,尽管有问题的标题,但此答案不会导致选择“无”;而是选择了第一个项目。 (这不是对答案的批评,只是应该澄清的事情。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    相关资源
    最近更新 更多