我有两个旋转器,当我在spinnner 1上选择“食肉动物”时,旋转器2包含数据“鳄鱼”,“老虎”。
但是当我选择“草食动物”旋转器1时,旋转器2包含数据“长颈鹿”,“牛”。
有没有人有类似上述情况的android代码?
答案 0 :(得分:0)
步骤涉及的是:为spinner1添加OnItemSelectedListener, 项目选择:用新数据集填充第二个
String dataSpinner1 [] = {"carnivore", "herbivore" };
String dataSpinner2 [][] = {{"crocodile", "Tiger"},{"giraffe", "cow"} };
spinner.setOnItemSelectedListener(this); // register OnItemSelectedListener
ArrayAdapter<String> spinnerArrayAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, dataSpinner1); // adapter for spinner1
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(spinnerArrayAdapter1);
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
populateAdapterForSecond(pos);
}
//set adapter for second
private void populateAdapterForSecond(int pos){
String spinnerArray [] = new String[2];
for(int i=0; i<2; i++){
spinnerArray[i] = dataSpinner2[pos][i] ;
}
ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray); // adapter for spinner1
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(spinnerArrayAdapter2);
}