Java 第二个微调器从不基于第一个微调器更改数组

Java 第二个微调器从不基于第一个微调器更改数组,java,android,spinner,android-spinner,Java,Android,Spinner,Android Spinner,我正在为Android构建一个有两个旋转器的应用程序。第一个有一系列的选择。我有一些代码来更改第二个微调器的值,但是数组在第二个微调器中从不更改。我选中了,selectedValue与selected值相等,因此必须使用ArrayAdapter 我的代码如下: StationList.java public class StationList extends Activity { @Override protected void onCreate(Bundle savedInstanc

我正在为Android构建一个有两个旋转器的应用程序。第一个有一系列的选择。我有一些代码来更改第二个微调器的值,但是数组在第二个微调器中从不更改。我选中了,selectedValue与selected值相等,因此必须使用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();
}





}
公共类StationList扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u station\u list);
最终喷丝器喷丝器1=(喷丝器)findViewById(R.id.Spinner1);
最终喷丝器喷丝器2=(喷丝器)findViewById(R.id.Spinner2);
最后一行红色字符串=this.getString(R.String.Red\u行);
最终字符串蓝色线=this.getString(R.String.Blue线);
字符串绿色线=this.getString(R.String.Green线);
String Orange\u Line=this.getString(R.String.Orange\u Line);
String Brown\u Line=this.getString(R.String.Brown\u Line);
String Pink\u Line=this.getString(R.String.Pink\u Line);
String Purple\u Line=this.getString(R.String.Purple\u Line);
字符串黄色线=this.getString(R.String.Yellow线);
TextView tv0=(TextView)findViewById(R.id.tv0);
最终文本视图tv11=(文本视图)findViewById(R.id.tv11);
tv0.setText(String.valueOf(红线));
//在操作栏中显示“向上”按钮。
getActionBar().setDisplayHomeAsUpEnabled(true);
Spinner1.setOnItemSelectedListener(新的OnItemSelectedListener(){
已选择公共视图(AdapterView arg0、视图arg1、,
整数arg2,长arg3){
//TODO自动生成的方法存根
字符串selectedValue=arg0.getSelectedItem().toString();
tv11.setText(String.valueOf(selectedValue));
if(selectedValue.equals(红线))
{
ArrayAdapter firstAdapter=新的ArrayAdapter(StationList.this,R.array.Red_线);
喷丝头2.固定接头(第一接头);
}
if(selectedValue.equals(蓝色线))
{
ArrayAdapter SecondAdapter=新的ArrayAdapter(StationList.this,android.R.layout.simple\u list\u item\u 1,R.array.Blue\u Line);
喷丝头2.固定接头(第二接头);
}
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
});
返回;
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.activity\u station\u list,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
//此ID表示“主页”或“向上”按钮
//活动中,将显示向上按钮。使用NavUtils允许用户
//在应用程序结构中向上导航一级。对于
//更多详细信息,请参见Android Design上的导航模式:
//
// http://developer.android.com/design/patterns/navigation.html#up-对背
//
NavUtils.navigateUpFromSameTask(本);
返回true;
}
返回false;
}
公共无效发送测试(视图a){
Intent Intent9=新Intent(StationList.this、TestStation.class);
startActivityForResult(意向9,0);
setContentView(右布局、测试站);
}
public void onBackPressed(){
startActivity(新意图(StationList.this、MainActivity.class));
完成();
}
}
activity\u station\u 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>


我希望你能给我任何帮助来解决这个问题。谢谢您的时间。

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

你的问题是,在你的匿名内部类中,它是一个OnItemSelectedListener,
这个
不再指活动,而是指的是监听器。将
替换为
StationList。此
和您的问题应该得到解决。

它仍然会给我一个错误,即
构造函数数组适配器(StationList,int,String)未定义
。它建议从中删除字符串Blue1和Red1。那么如何输入设置Spinner2的字符串?您需要检查ArrayAdapter构造函数以确保使用了适当的参数。检查文档。值得注意的是,您需要传入一个字符串数组,而您不是。我该怎么做?我在strings.xml文件中定义了一个字符串数组。我相信有一个fromResource()函数可以从资源中提取对象。现在没有时间给你一个确切的链接,但我会在回家后检查。我更新了我的Question和代码,因为第二个微调器从未收到第一个微调器的信息。你能帮个忙吗?它仍然给我一个错误,构造函数ArrayAdapter(StationList,int,String)未定义。它建议从中删除字符串Blue1和Red1。那么如何输入设置Spinner2的字符串?您需要检查ArrayAdapter构造函数以确保使用了适当的参数。检查文档。值得注意的是,您需要传入一个字符串数组,而您不是。我该怎么做?我在strings.xml文件中定义了一个字符串数组。我相信有一个fromResource()函数可以从资源中提取对象。现在没有时间给你一个确切的链接,但我会在回家后检查。我更新了我的Question和代码,因为第二个微调器从未收到第一个微调器的信息。你能帮忙吗