【发布时间】:2017-07-21 04:59:01
【问题描述】:
我试图创建一个微调器,它显示数组列表中的数据列表。
当我单击下拉列表时,它会显示列表,但是当我单击下拉列表中的某个项目时,它不会显示微调器上的值。
我错过了什么吗?
注意:昨天我尝试使用 Log.d() 和 System.out.println 进行检查,itemOnSelected() 没有,但今天它工作正常。也许我正在重建项目,或者我在代码中更改了某些内容,但在我单击微调器内的项目后,微调器上的值仍然没有显示。
Spinner spnSubjectIDInfo;
ArrayList<String> subjectList;
ArrayAdapter<String> adpSubj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
subjectList = new ArrayList<String>();
subjectList .add("John");
subjectList .add("Maxi");
subjectList .add("Jeni");
spnSubjectIDInfo = (Spinner) v.findViewById(R.id.spnSubjectIDInfo);
adpSubj = new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_spinner_item, subjectList);
adpSubj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnSubjectIDInfo.setAdapter(adpSubj);
spnSubjectIDInfo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
XML
<Spinner
android:id="@+id/spnSubjectIDInfo"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginTop="54dp"
android:layout_centerHorizontal="true" />
【问题讨论】:
-
你可以尝试将“Toast.makeText(this..”改为“Toast.makeText(getBaseContext()”)
-
'getItemAtPosition' 方法返回一个对象。您需要调用“toString()”方法来获取该对象的字符串表示形式。
-
发布您的完整代码
-
因为您尝试设置 2 次微调器(下拉)视图:adpSubj = new ArrayAdapter
(this, android.R.layout.simple_spinner_item, subjectList); adpSubj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);在适配器中只设置一次不需要setDropdownviewresource -
问题是您使用的是微调器 id,您必须为微调器类 ex 绑定微调器 id;微调器 spnSubjectIDInfo = (微调器) findViewById(R.id.spinner);
标签: android android-arrayadapter android-spinner android-xml android-adapter