1

我正在构建一个有两个微调器的 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>

我很想你能给我解决这个问题的任何帮助。感谢您的时间。

4

1 回答 1

1

您的问题是,在您的匿名内部类中,它是一个 OnItemSelectedListener,this不再指代活动,而是指向侦听器。替换thisStationList.this,您的问题应该得到解决。

于 2013-09-30T22:05:41.663 回答