Android 微调器,获取 selectedItem

Android spinner, get selectedItem

正如我提到的,我正在开发一个 "book an appointment" 应用程序。在其中,它有一个微调器,用户可以在其中 select 的地方。 在 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 中,我想到达 selected 项目,所以我尝试了

public void setPlace(String place){
    this.place = place;
}

并且在 submit.onClick 中有一个 BookApp(user, place, completeDate); 在调试模式下,为什么在 onClick 中 place 会变成 null。但是即使我改变了 spinnersetPlace 是 运行,而 this.place 将等于原来的 place,但在它之后 place 等于null。在 BookAppointment.java 中有一个全局 public String place; 并且我在 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();

这应该 return 您从微调器中选择的字符串。