2013-09-30 29 views
1

我正在爲Android安裝具有兩個spinners的應用程序。第一個有一系列的選擇。我有一些代碼來更改第二個微調器的值,但該數組在第二個微調器中永遠不會改變。我檢查過,並且selectedValue與選定的值相等,所以它必須與ArrayAdapter一起使用。Second Spinner永遠不會根據First Spinner更改數組

我的代碼如下:

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

你的問題是,你的匿名內部類是一個OnItemSelectedListener,this不再指向Activity,它指向了監聽器。將this替換爲StationList.this,並解決您的問題。

+0

它仍然給我'構造函數ArrayAdapter (StationList,int,String)未定義的錯誤。它建議從中刪除字符串Blue1和Red1。那麼我如何設置Spinner2的字符串呢? – hichris123

+0

您需要檢查您的ArrayAdapter構造函數以確保使用適當的參數。檢查文檔[這裏](http://developer.android.com/reference/android/widget/ArrayAdapter.html)。值得注意的是,你需要傳入一個字符串數組,而你並不是。 –

+0

我該怎麼做?我有一個在我的strings.xml文件中定義的字符串數組。 – hichris123