任何人都可以帮助我吗?
spinner2没有显示所选位置,其中必须使用Toast(代码下方)显示。
我无法找到我的代码有什么问题。非常感谢您的回复。谢谢。
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5.67"
android:entries="@array/groupLamp />
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*parameters*/
final Integer incandescant[]={5,15,25,35,40,60,75,100,125,150};
final Integer led[] = {0,2,3,5,8,13,15,20,23,28};
final Integer cfl[] = {0,0,0,0,13,15,25,30,40,45};
final Integer fluorescent[] = {40,40,20,55};
Button btnCalc = (Button) findViewById(R.id.btnCalculate);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("Incandescant");
list.add("CFL");
list.add("Flourescent");
list.add("Others");
final List<String> list2 = new ArrayList<String>();
final List<String> list3 = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
list2.clear();
if(pos==0){
for(Integer ctr: incandescant){
list2.add(ctr+ " watts");
}
}else if(pos==1){
for(Integer ctr: led){
list2.add(ctr +" watts");
}
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter2);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(MainActivity.this, ""+pos, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
答案 0 :(得分:0)
看起来很奇怪 - 您在XML中的spinner的1 ID是spinner2
,但在代码中您引用的是spnExistLamp
和spnElampWat
。因此,XML和代码之间没有任何联系。
答案 1 :(得分:0)
确保您正确链接xml中的视图。
xml中的 android:id="@+id/spinner2"
但是在你的代码中你正在使用
Spinner spinner1 = (Spinner) findViewById(R.id.spnExistLamp);
final Spinner spinner2 = (Spinner) findViewById(R.id.spnElampWat);
Plz在您的代码中进行以下更改
final List<String> list2 = new ArrayList<String>();
final ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list2); //------ line location changed.
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
list2.clear();
if(pos==0){
for(Integer ctr: incandescant){
list2.add(ctr+ " watts");
}
}else if(pos==1){
for(Integer ctr: led){
list2.add(ctr +" watts");
}
}
dataAdapter2.notifyDataSetChanged(); //------- line added
}
public void onNothingSelected(AdapterView<?> parent) {
}
});