【问题标题】:Set Value of spinner from Array in strings.xml在 strings.xml 中从数组中设置微调器的值
【发布时间】:2011-12-15 22:59:21
【问题描述】:

我有一个微调器,它使用来自 strings.xml 的数组

如果数组有 5 个字符串 (1,2,3,4,5),我希望微调器显示第二个 字符串(2)作为默认值,这可能吗? 我知道我可以重新排列字符串顺序,以便第一个是 2, 但如果微调器对话框显示为 (2,1,3,4,5),这看起来不太好。

或者是否必须以编程方式在我的活动中创建数组 然后使用 setPostion()? 我已经尝试过了,但是在活动中创建数组时出错。 谁能给我一个如何创建数组和使用的例子 它在 spinner() 中

我也在这里搜索过答案,但似乎找不到我需要的。

感谢观看....

【问题讨论】:

    标签: android


    【解决方案1】:

    我建议你检查:http://developer.android.com/resources/tutorials/views/hello-spinner.html

    您应该在 Activity 中创建一个 ArrayAdapter。

    从上面的链接:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }
    

    你可以使用

    spinner.setSelection(adapter.getPosition(value)));
    

    设置位置。

    【讨论】:

      【解决方案2】:

      为回复干杯.....

      没有做我需要的,但设法解决了我的问题如下..

      首先我在我的活动中创建了一个数组,而不是 strings.xml。

          String[] NoCore_Array = new String [5];
      { 
          NoCore_Array[0] = "1";
          NoCore_Array[1] = "2";
          NoCore_Array[2] = "3";
          NoCore_Array[3] = "4";
          NoCore_Array[4] = "5";
      
      }
      

      然后我使用...创建了微调器

          Spinner spnNoCore = (Spinner) findViewById(R.id.spinner_nocore);
      

      然后创建适配器,使用上面的数组....

          ArrayAdapter NoCoreAdapter =  new ArrayAdapter(this, 
          android.R.layout.simple_spinner_item,  NoCore_Array);
          spnNoCore.setAdapter(NoCoreAdapter);
      

      然后设置适配器的默认位置如下...

          //Set Default Selection
          spnNoCore.setSelection(1);
      

      然后是用于操作的其余微调器代码...

           spnNoCore.setOnItemSelectedListener(new OnItemSelectedListener(){
      
              public void onItemSelected(AdapterView<?> parent,
                  View view, int pos, long id) {
      
                  //Get item from spinner and store in string conductorSize........
                  NoCore = parent.getItemAtPosition(pos).toString();
      
                  if (NoCore.equals(NoCore1))   { CoreNo = 1    ;  }
                  if (NoCore.equals(NoCore2))   { CoreNo = 2    ;  }
                  if (NoCore.equals(NoCore3))   { CoreNo = 3    ;  }
                  if (NoCore.equals(NoCore4))   { CoreNo = 4    ;  }
                  if (NoCore.equals(NoCore5))   { CoreNo = 5    ;  }
      
           }
      
              public void onNothingSelected(AdapterView parent) {
                // Do nothing.
              }
          });
      

      希望这可以帮助其他遇到同样问题的人 在 Spinner 上设置默认选择。

      但是,如果这样做,对话框的布局就不那么好了, 缺少单选按钮,看起来像文本选择 用线分隔各个部分。

      【讨论】:

      • thanx 伙计,你不知道你为我节省了多少工作,我尝试了很多无用的解决方案,比如创建旋转数组
      • @Coops5575,您的解决方案中的对话框没有单选按钮的事实是由于您使用 simple_spinner_item 而不是 simple_spinner_dropdown_item 创建了适配器,仅此而已。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多