【发布时间】:2016-03-15 19:21:04
【问题描述】:
正如我所说,我正在开发一个“预约”应用程序。在其中,它有一个微调器,用户可以在其中选择位置。
在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
}
}
在BookAppointment.java 中我想到达所选项目,所以我尝试了
public void setPlace(String place){
this.place = place;
}
在submit.onClick 中有一个BookApp(user, place, completeDate);
在调试模式下,为什么在 onClick 中place 将是null。但即使我更改了spinner,setPlace 也在运行,this.place 将等于原来的place,但在它之后不知何故place 等于null。在 BookAppointment.java 中有一个全局 public String place; ,而我在 BookAppointment.java 中无处使用 place。请帮助我,我错过了什么?我做错了什么?
点击:
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
}
});
}
【问题讨论】:
标签: java android spinner onitemselectedlistener