【问题标题】:Android Spinner: Populate Second Spinner Based on Selection of FirstAndroid Spinner:根据第一个的选择填充第二个 Spinner
【发布时间】:2017-03-25 14:11:57
【问题描述】:

我的要求是根据第一个微调器的选择填充第二个微调器。我的问题是:是否可以有一个具有节点结构的 XML 文件?例如这样的:

<Europe>
<State name="France">
     <city name="Paris"/>
     <city name="Lyon"/>
     <city name="Nice"/>
</State>
<State name="Spain">
     <city name="Madrid"/>
     <city name="Barcelona"/>
     <city name="Valencia"/>
</State>
<State name="Germany">
     <city name="Berlin"/>
     <city name="Frankfurt"/>
     <city name="Monaco"/>
</State>
</Europe>

如何做到这一点?请帮我。谢谢

【问题讨论】:

  • 提示:是的,有可能,,,只需解析这个 xml 文件并将其放入 ArrayList 并将其设置为第二个微调器
  • 谢谢 Samir.. 解析 xml 后,我将在第一个微调器中获得州名,在第二个中获得与所选州相关联的城市?如何做到这一点?

标签: android xml xml-parsing android-spinner


【解决方案1】:

您可以设置一个 onItemSelectedListener 并根据所选值更新第二个微调器的适配器内容,例如:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this); 
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);

//setup initial adapters etc.

 public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
       //based on the selected item update the second spinner:
 ArrayAdapter<CharSequence> adapter ArrayAdapter.createFromResource(this,
        R.array.planets_array, android.R.layout.simple_spinner_item);

// Apply the adapter to the spinner
spinner2.setAdapter(adapter);
    }  

您还可以使用 updateItems(List items) 方法为您的微调器 2 使用自定义适配器来处理项目更改,例如该方法如下所示:

    public void updateItems(List<String> items){
      currentItems.clear();
      currentItems.addAll(items);
    notifyDatasetChanged();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多