android更改微调器的列表取决于另一个微调器

时间:2014-09-03 22:57:05

标签: android arrays spinner

我的活动中有几个微调器

是否可以根据在微调器1上选择的项目来更改微调器2的内容

纺纱机如下

          <Spinner
          android:id="@+id/spinner1"
          android:layout_width="0sp"
          android:layout_weight="0.6"
          android:layout_height="wrap_content"
          android:layout_marginLeft="10sp"
          android:layout_marginTop="10sp"
          android:contentDescription="Fred"
          android:entries="@array/country_arrays"
          android:prompt="@string/country_prompt" />


      <Spinner
      android:id="@+id/spinner2"
      android:layout_width="0sp"
      android:layout_weight="0.4"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10sp"
      android:layout_marginTop="10sp"
      android:contentDescription="Fred"
      android:entries="@array/country_arrays2"
      android:prompt="@string/country_prompt2" />

如果在微调器1上选择了第3项,如何将country_arrays2中的条目更改为说明微调器2中的国家/地区数组

任何帮助表示赞赏

标记

1 个答案:

答案 0 :(得分:0)

在java活动文件中,您可以通过编程方式加载微调器内容:

    Spinner miSpinner = (Spinner) findViewById("spinner2");

    ArrayAdapter<String> tiposElementos = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);
    tiposElementos.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    for(int i=0; i<20; i++) {
        tiposElementos.add("Element" + i);
    }

    miSpinner.setAdapter(tiposElementos); 

此外,您可以控制所选项目的更改:

miSpinner.setOnItemSelectedListener( new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int index, long id) {
        your code
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) { }
};
相关问题