2017-03-25 149 views
0

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

<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> 

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

+0

提示:是其可能的,,,只是解析这个XML文件并把它的ArrayList并将其设置为第二次纺织呃 –

+0

谢谢萨米尔..解析XML后,我将在第一个微调状态名称,并在第二个相关城市链接到选定的国家?这个怎么做? –

回答

0

您可以设置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); 
    } 

您也可以为您的微调2自定义适配器与updateItems (列表项)方法照顾的项目变化,例如该方法看起来是这样的:

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