Android 微调器,未选择任何内容

标签 android spinner selection

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

@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;
    }

最佳答案

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

spinner.setSelection(0);

然后设置监听器:

spinner.setOnItemSelectedListener(this);

这只需要在创建微调器时完成一次。通过这种方式,您可以避免对 OnItemSelectedListener 进行不必要的调用,默认调用一次。

关于Android 微调器,未选择任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25556371/

相关文章:

android - 在 Android 中启动时启动 apk

android - 如何在android webview中模拟特定坐标处的点击?

Android Spinner 不显示列表项

python - QGraphicsScene 将选择保留在空选择框上

以英寸为单位的 android 设备尺寸和与密度无关的像素 (dp) 计数

android - 如何保存 onClickListener 的状态?

android - 如何从 fragment 中将微调器置于操作栏的中心

java - MultiAutoCompleteTextView 未在 AlertDialog 中显示下拉列表

android - 如何更改屏幕上的文本选择小部件?

javascript - 数据表作为全功能的表单元素