第二个Spinner永远不会根据First Spinner更改数组

时间:2013-09-30 22:01:41

标签: java android spinner android-spinner

我正在为拥有两个微调器的Android开发应用程序。第一个有一系列选择。我有一些代码来更改第二个微调器的值,但是数组在第二个微调器中永远不会改变。我检查过,并且selectedValue与所选值相等,因此它必须与ArrayAdapter一起使用。

我的代码如下:

StationList.java

  public class StationList extends Activity {



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

    final Spinner Spinner1 = (Spinner) findViewById(R.id.spinner1);
    final Spinner Spinner2 = (Spinner) findViewById(R.id.spinner2);
    final String Red_Line = this.getString(R.string.Red_Line);
    final String Blue_Line = this.getString(R.string.Blue_Line);
    String Green_Line = this.getString(R.string.Green_Line);
    String Orange_Line = this.getString(R.string.Orange_Line);
    String Brown_Line = this.getString(R.string.Brown_Line);
    String Pink_Line = this.getString(R.string.Pink_Line);
    String Purple_Line = this.getString(R.string.Purple_Line);
    String Yellow_Line = this.getString(R.string.Yellow_Line);
    TextView tv0 = (TextView) findViewById(R.id.tv0);
    final TextView tv11 = (TextView) findViewById(R.id.tv11);

    tv0.setText(String.valueOf (Red_Line));



    // Show the Up button in the action bar.
    getActionBar().setDisplayHomeAsUpEnabled(true);

    Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub



                String selectedValue = arg0.getSelectedItem().toString();
                tv11.setText(String.valueOf (selectedValue));
                if(selectedValue.equals(Red_Line))
                {
                    ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(StationList.this,R.array.Red_Line);

                    Spinner2.setAdapter(firstAdapter);
                }

              if(selectedValue.equals(Blue_Line))
               {
                  ArrayAdapter<String> SecondAdapter = new ArrayAdapter<String>(StationList.this,android.R.layout.simple_list_item_1,R.array.Blue_Line);
                  Spinner2.setAdapter(SecondAdapter);

            }
        }

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

        }
    });
return;
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_station_list, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return false;

}






public void sendTest(View a) {
    Intent Intent9 = new Intent(StationList.this, TestStation.class);
    startActivityForResult(Intent9, 0); 
    setContentView(R.layout.test_station);
    }






public void onBackPressed(){

startActivity(new Intent(StationList.this, MainActivity.class));
finish();
}





}

activity_station_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
tools:context=".Station List" >
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="17dp"
    android:layout_marginTop="24dp"
    android:text="@string/line" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="32dp"
    android:text="@string/station" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="58dp"
    android:layout_marginRight="58dp"
    android:entries="@array/Lines" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="36dp"
    android:layout_toRightOf="@+id/textView2"
    android:text="@string/Check"
    android:onClick="sendTest" />

<Spinner
    android:id="@+id/spinner2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="62dp"
    android:layout_marginRight="59dp" />

 </RelativeLayout>

我很乐意帮助你解决这个问题。谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

您的问题是,在您的匿名内部类中,OnItemSelectedListener,this不再引用Activity,它指向侦听器。将this替换为StationList.this,您的问题应该得到解决。