Android工作室第二个旋转器卡在选择的第一选择

时间:2016-07-21 09:56:22

标签: android spinner android-spinner

我有两个旋转器,第二个旋转器连接到第一个旋转器。第二个旋转器下降,但总是卡在第一个选择,并不断显示Toast。

如何修复第二个微调器onItemSelected和Toast?

谢谢

content_main.xml
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Select program:" />
    <Spinner
        android:id="@+id/program_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/program_arrays" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Select course:" />
    <Spinner
        android:id="@+id/course_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"  />
</LinearLayout>

-

MainActivity.java
public class MainActivity extends AppCompatActivity implements
    AdapterView.OnItemSelectedListener {
Spinner program_spinner, course_spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    program_spinner = (Spinner)findViewById(R.id.program_spinner);
    course_spinner = (Spinner)findViewById(R.id.course_spinner);

    program_spinner.setOnItemSelectedListener(this);
    course_spinner.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                           long arg3) {
    String program = String.valueOf(program_spinner.getSelectedItem());
    String course = String.valueOf(course_spinner.getSelectedItem());
    switch(program) {
        case "Accounting":
            ArrayAdapter<CharSequence> dataAdapter = ArrayAdapter.createFromResource(
                    this, R.array.accounting_course_arrays, android.R.layout.simple_spinner_item);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            dataAdapter.notifyDataSetChanged();
            course_spinner.setAdapter(dataAdapter);
            break;
        case "Nursing":
            ArrayAdapter<CharSequence> dataAdapter2 = ArrayAdapter.createFromResource(
                    this, R.array.nursing_course_arrays, android.R.layout.simple_spinner_item);
            dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            dataAdapter2.notifyDataSetChanged();
            course_spinner.setAdapter(dataAdapter2);
            break;
    }
    Toast.makeText(getApplicationContext(), program, Toast.LENGTH_SHORT).show();
    Toast.makeText(getApplicationContext(), course, Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
}

}

2 个答案:

答案 0 :(得分:0)

尝试删除dataAdapter.notifyDataSetChanged();dataAdapter2.notifyDataSetChanged();并运行。这可能有所帮助。

答案 1 :(得分:0)

使用以下内容更改上述代码,并尝试首先正确学习基础知识:

public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
   Spinner program_spinner, course_spinner;
   ArrayAdapter<CharSequence> dataAdapter=null;
   ArrayAdapter<CharSequence> dataAdapter2=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

program_spinner = (Spinner)findViewById(R.id.program_spinner);
course_spinner = (Spinner)findViewById(R.id.course_spinner);

dataAdapter = ArrayAdapter.createFromResource(
                this, R.array.accounting_course_arrays, android.R.layout.simple_spinner_item);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

dataAdapter2 = ArrayAdapter.createFromResource(
                this, R.array.nursing_course_arrays, android.R.layout.simple_spinner_item);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);



program_spinner.setOnItemSelectedListener(this);
course_spinner.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                       long arg3) {
switch(arg1.getId()) {
    case R.id.program_spinner:
         String program = String.valueOf(program_spinner.getSelectedItem());

    switch(program) {
    case "Accounting":
        course_spinner.setAdapter(dataAdapter);
        break;
    case "Nursing":
        course_spinner.setAdapter(dataAdapter2);
        break;
        }
         //do whatever when selecting item from your first spinner
        break;
    case R.id.course_spinner:
        //do whatever when selecting item from your secondspinner
        break;
}

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
}

}