我試圖創建一個顯示數組列表中的數據的微調器。ArrayList不顯示在Spinner上
當我點擊下拉菜單時,它會顯示列表,但是當我點擊下拉列表中的某個項目時,它不會顯示微調器上的值。
我在這裏錯過了什麼嗎?
注意:昨天我試圖檢查使用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(這個..」 到 「Toast.makeText(getBaseContext()」 –
'getItemAtPosition' 方法返回一個對象,你需要調用「的toString()方法以獲得該對象的字符串表示形式 – ADimaano
發佈完整代碼 – MinnuKaAnae