I’ve a spinner set on a screen to which I pass a string from another screen. I want that string to be set as spinner’s default. I’ve looked into posts and tried to implement it but somehow it’s not working.
Here’s my code:
//workRequestFetched is a private String object
workRequestFetched = extras.getString("workRequest");
workRequestSpinner = (Spinner) findViewById(R.id.workRequestSpinner);
//ServiceCall is a function call to the web service it works fine otherwise, please don't worry about it.
ArrayList<String> workRequests = ServiceCall.workRequests;
ArrayAdapter<String> workRequestAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
workRequests);
selected is boolean which checks if the string has been passed or not. I’ve print it out to check if the execution thread enters the if clause and it does, so think the problem isn’t there.
if(selected){
System.out.println("Entered selected- "+workRequestFetched);
//This is what supposed to set the spinner's default to the position of the string right? but it doesn't work!
int spinnerPosition = workRequestAdapter.getPosition(workRequestFetched);
workRequestSpinner.setSelection(spinnerPosition);
}
workRequestAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
workRequestSpinner.setAdapter(workRequestAdapter);
Could anyone point out the mistake? Thank you!
Change your snippet to following