2

I am working on android application in which i am getting string values from server. I want to set display text of my spinner from string value coming from server. For example if it String value from server is "O+" then the display text of the spinner should be "O+". Given below is my code, please guide me for this.

 private String[] state = { "O-", "O+", "A-", "A+", "AB-", "AB+", "B-", "B+" }; Spinner spinnerOsversions; spinnerOsversions = (Spinner) findViewById(R.id.spinner_BloodGroup); ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, state); adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerOsversions.setAdapter(adapter_state); spinnerOsversions.setOnItemSelectedListener(this); 
2
  • 1
    Get values from server with help of a webservice and store in an array of strings / objects and use adapter for same. Commented Nov 30, 2014 at 19:07
  • 1
    Thanks @KanakSony for your reply. Can you please give me some little bit code snippet according to my code, assume the coming value from server to be "O+" Commented Nov 30, 2014 at 19:12

2 Answers 2

3

Suppose your server text contains as one of its choices: "O+".

To find and compare the position of "O+" in the Spinner use this:

 String CompareValue= "O+"; if (!CompareValue.equals(null)) { int SpinnerPostion = adapter_state.getPosition(CompareValue); spinnerOsversions.setSelection(SpinnerPostion); SpinnerPostion = 0; } 
Sign up to request clarification or add additional context in comments.

2 Comments

works like a charm.. Thank you so much. Can you pleas just explain me the meaning of (!CompareValue.equals(null))? Why you are comparing it with null value?
Just for avoid null pointer exception.. Incase your server value return error.
2
 String[] stateArray = { "O-", "O+", "A-", "A+", "AB-", "AB+", "B-", "B+" }; String statePosition = ""; for (int i = 0; i < stateArray.length; i++) { if (stateArray[i].equals(result.getStateSelected())) { // result.getStateSelected() is your service response statePosition = stateArray[i]; return ; } } spinnerOsversions.setSelection(adapter.getPosition(statePosition), true); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.