【发布时间】:2016-07-20 02:45:20
【问题描述】:
为此苦苦挣扎,这就是我所拥有的。
微调器 1 数组。
<!--for spinner 1-->
<string-array name="Manu_array">
<item>Manu 1xsd</item>
<item>Manu 2xrsd</item>
<item>Manu 3x4rsd</item>
</string-array>
Spinner 2 数组。
<!--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>
所以微调器 1 我会选择“Manu 3x4rsd”然后微调器 2“xs5”我会吐司“Manu 3x4rsd”+“xs5”
我选择 Spinner 1 的 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
}
});
我的 Spinner 2 选择的 Java 代码
final Spinner Spinner2= (Spinner) findViewById(R.id.Spinner2);
Spinner1[0].setOnItemSelectedListener(new adapterView.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_array;
break;
case 1:
arrayId = R.array.Manu_2xrsd;
break;
case 2:
arrayId = R.array.Manu_3x4rsd.array;
break;
};
ArrayAdapter<CharSequence> adapterL = ArrayAdapter.createFromResource(settings.this, arrayId, R.layout.textview);
Spinner1.setAdapter(adapterL);
String Spinner1 = Spinner1.getSelectedItem().toString();
Toast.makeText(getBaseContext(), "You have selected : " + Spinner1,
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
问题是,虽然我可以看到在微调器 2 中填充的值并滚动浏览它们,并且看起来我可以选择它们,但我没有代码可以在选择它们和生成 toast 时做任何事情。我试图在微调器中创建一个侦听器,但我无法做任何事情来让它按需要运行。
所以我怀疑我正在以完全错误的方式解决问题。
一旦我可以烘烤微调器 1 和微调器 2 选定的值,我应该能够将它们组合起来以从字符串列表中读取,从而将它们组合成一个命令。
想要使用微调器来获得视觉效果,而不是为了选择所需的值而滚动浏览一个很大的列表。
【问题讨论】:
标签: android arrays string spinner bind