Usman Khan
Usman Khan

Reputation: 3973

How to set Spinner text in android coming from server

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);

Upvotes: 2

Views: 956

Answers (2)

stacktry
stacktry

Reputation: 322

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;
    }

Upvotes: 3

JpCrow
JpCrow

Reputation: 5077

    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);

Upvotes: 2

Related Questions