Java Android spinner,获取selectedItem
我一开始就在开发一个“预约”应用程序。它有一个微调器,用户可以在其中选择位置。 在Java Android spinner,获取selectedItem,java,android,spinner,onitemselectedlistener,Java,Android,Spinner,Onitemselectedlistener,我一开始就在开发一个“预约”应用程序。它有一个微调器,用户可以在其中选择位置。 在BookAppointment.java中,这是微调器代码: public void addListenerOnSpinnerItemSelection() { pspinner = (Spinner) findViewById(R.id.spinner1); pspinner.setOnItemSelectedListener(new CustomOnItemSelectedListener())
BookAppointment.java
中,这是微调器代码:
public void addListenerOnSpinnerItemSelection() {
pspinner = (Spinner) findViewById(R.id.spinner1);
pspinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
CustomOnItemSelectedListener.java
:
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
BookAppointment ba = new BookAppointment();
String place;
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
Toast.makeText(parent.getContext(),
"OnItemSelectedListener: " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
place = parent.getItemAtPosition(pos).toString(); //the actual selected item of spinner
ba.setPlace(place);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
在submit.onClick
中有一个BookApp(用户、地点、完成日期)代码>
在调试模式下,onClick中的位置将为null
。但即使我更改了微调器
,也会运行设置位置
,并且此.place
将等于原始位置
,但在它之后,为什么位置
等于空值
。在BookAppointment.java
中有一个全局公共字符串位置
和我在BookAppointment.java中没有其他地方使用place。请帮帮我,我错过了什么?我做错了什么
onClick:
public void addListenerOnButton() {
btnSubmitP = (Button) findViewById(R.id.btnSubmitP);
pspinner = (Spinner) findViewById(R.id.spinner1);
btnSubmitP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*May I should place here the spinner's findViewById ?*/
place = pspinner.getSelectedItem().toString(); //to get the actual selected place
String completeDate = date + " " + itemValue;
Toast.makeText(BookAppointment.this,
"OnClickListener : " +
"\nHelyszín: " + place + //it prints the actual selected item
"\nDátum: " + completeDate,
Toast.LENGTH_SHORT).show();
User user = userLocalStore.getLoggedInUser();
if (user == null) {
Toast.makeText(BookAppointment.this,
"user = null",
Toast.LENGTH_SHORT).show();
}
BookApp(user, place, completeDate); //Here the debug not display the place, nothin like place=null, nothing, only the user and completeDate's value
}
});
}
尝试在submit.onClick中添加此选项:
String places= spinnerReferenceName.getSelectedItem().toString();
这将返回spinner中选择的字符串。我尝试过,但有些原因是它仅在活动开始时才起作用。在微调器中的任何编辑(如选择另一项)未显示后,位置保持原始的第一项:S。我已将该位置放在onClick之外,现在它在里面,但为什么应用程序只是跳过它,好像永远不会在那里。oNow,它工作了,谢谢!然而,调试跳过了这个位置,但是在BookApp()
中,这个位置是正确的!我应该把找到的视频放在onclick外面!非常感谢。
String places= spinnerReferenceName.getSelectedItem().toString();