As once I mentoined I am developing a "book an appointment" application. And in it, it have a spinner, where the user can select the place.
In BookAppointment.java
this is the spinner code:
public void addListenerOnSpinnerItemSelection() {
pspinner = (Spinner) findViewById(R.id.spinner1);
pspinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
The 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
}
}
And in BookAppointment.java
I want to reach the selected item so I tried
public void setPlace(String place){
this.place = place;
}
And in the submit.onClick
there is a BookApp(user, place, completeDate);
And in debug mode somewhy in the onClick the place
will be null
. But even I change the spinner
, the setPlace
is run, and this.place
will equal the original place
, but after it somewhy place
equals null
. In BookAppointment.java
there is a global public String place;
and I nowhere else use place in BookAppointment.java . Please help me, what I missed? What I did wrong?
The 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
}
});
}
Try adding this in submit.onClick:
String places= spinnerReferenceName.getSelectedItem().toString();
This should return you the selected string from spinner.