Spinner1 从数组填充,然后 select 基于第一个 Spinner selection 的 Spinner2 的第二个数组
Spinner1 populate from array, then select second array for spinner2 based on 1st spinner selection
好的微调器 1 使用 Manu_array
微调器 2 使用 Manu 1xsd、Manu 2xrsd 或 3x4rsd
我现在需要的是能够填充第二个微调器,从微调器 1.
中的 Manu_array 中选择一个值后
<!--for spinner 1-->
<string-array name="Manu_array">
<item>Manu 1xsd</item>
<item>Manu 2xrsd</item>
<item>Manu 3x4rsd</item>
</string-array>
<!--for spinner 2-->
<string-array name="Manu 1xsd">
<item>a1</item>
<item>a2</item>
<item>a3</item>
<item>a4</item>
</string-array>
<string-array name="Manu 2xrsd">
<item>bg 1</item>
<item>bg 2</item>
</string-array>
<string-array name="Manu 3x4rsd">
<item>z1</item>
<item>z2</item>
<item>zd4</item>
<item>xs5</item>
<item>fg34</item>
</string-array>
我的java文件代码:
final Spinner[] spinner1 = {(Spinner) findViewById(R.id.spinner1)};
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Manu_array, R.layout.textview);
spinner1[0].setAdapter(adapter);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1[0].setAdapter(adapter);
spinner1[0].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
// storing string resources into Array
Manu= getResources().getStringArray(R.array.Manu_array);
Toast.makeText(getBaseContext(), "You have selected : " + Manu[index],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
});
我可以处理允许微调器填充数组数据的代码,但我无法让第二个微调器根据第一个微调器的选择进行填充,如果等于值使用数组肯定很简单。
上面的微调器显示了 Manu_array 的值,下一个微调器必须采用所选的值并填充适当的数组值,但我设法做的就是生成第二个微调器我手动输入的数组值,未选择。
if(Manu.equals("Manu 1xsd")){spinner2 languages = getResources().getStringArray(R.array.Manu 1xsd_array)}else......
任何我可以寻找示例的建议,请看这里的一些,让我感到困惑。
在单击第一个微调器中的项目时设置第二个微调器适配器
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int arrayId = 0;
switch(position) {//get array id according to selected position
case 0:
arrayId = R.array.Manu_1xsd;
break;
case 1:
arrayId = R.array.Manu_2xrsd;
break;
case 2:
arrayId = R.array.Manu_3x4rsd;
break;
};
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, arrayId, R.layout.textview);
spinner2.setAdapter(adapter);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
好的微调器 1 使用 Manu_array 微调器 2 使用 Manu 1xsd、Manu 2xrsd 或 3x4rsd 我现在需要的是能够填充第二个微调器,从微调器 1.
中的 Manu_array 中选择一个值后<!--for spinner 1-->
<string-array name="Manu_array">
<item>Manu 1xsd</item>
<item>Manu 2xrsd</item>
<item>Manu 3x4rsd</item>
</string-array>
<!--for spinner 2-->
<string-array name="Manu 1xsd">
<item>a1</item>
<item>a2</item>
<item>a3</item>
<item>a4</item>
</string-array>
<string-array name="Manu 2xrsd">
<item>bg 1</item>
<item>bg 2</item>
</string-array>
<string-array name="Manu 3x4rsd">
<item>z1</item>
<item>z2</item>
<item>zd4</item>
<item>xs5</item>
<item>fg34</item>
</string-array>
我的java文件代码:
final Spinner[] spinner1 = {(Spinner) findViewById(R.id.spinner1)};
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Manu_array, R.layout.textview);
spinner1[0].setAdapter(adapter);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1[0].setAdapter(adapter);
spinner1[0].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
// storing string resources into Array
Manu= getResources().getStringArray(R.array.Manu_array);
Toast.makeText(getBaseContext(), "You have selected : " + Manu[index],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
});
我可以处理允许微调器填充数组数据的代码,但我无法让第二个微调器根据第一个微调器的选择进行填充,如果等于值使用数组肯定很简单。
上面的微调器显示了 Manu_array 的值,下一个微调器必须采用所选的值并填充适当的数组值,但我设法做的就是生成第二个微调器我手动输入的数组值,未选择。
if(Manu.equals("Manu 1xsd")){spinner2 languages = getResources().getStringArray(R.array.Manu 1xsd_array)}else......
任何我可以寻找示例的建议,请看这里的一些,让我感到困惑。
在单击第一个微调器中的项目时设置第二个微调器适配器
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int arrayId = 0;
switch(position) {//get array id according to selected position
case 0:
arrayId = R.array.Manu_1xsd;
break;
case 1:
arrayId = R.array.Manu_2xrsd;
break;
case 2:
arrayId = R.array.Manu_3x4rsd;
break;
};
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, arrayId, R.layout.textview);
spinner2.setAdapter(adapter);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});