如何设置来自服务器的android中的Spinner文本

时间:2014-11-30 19:00:46

标签: android android-spinner

我正在研究android应用程序,其中我从服务器获取字符串值。我想从来自服务器的字符串值设置我的微调器的显示文本。例如,如果它来自服务器的字符串值是" O +"然后旋转器的显示文本应为" O +"。以下是我的代码,请指导我。

        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 个答案:

答案 0 :(得分:3)

假设您的服务器文本包含其中一个选项:&#34; O +&#34;。

查找并比较&#34; O +&#34;的位置在Spinner中使用这个:

 String  CompareValue= "O+";

if (!CompareValue.equals(null)) {
        int SpinnerPostion = adapter_state.getPosition(CompareValue);
       spinnerOsversions.setSelection(SpinnerPostion);
        SpinnerPostion = 0;
    }

答案 1 :(得分: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);