【问题标题】:Retrieve Object From Spinner Array And Set It从微调器数组中检索对象并设置它
【发布时间】:2015-05-27 04:17:14
【问题描述】:

我正在尝试从我的微调器数组中检索一个对象并将其设置为某个常量,在本例中为“EFFECT_AQUA”

我的数组

String[] spinnerValues = {"Aqua", "Mono", "Blackbird", "Negative"};

当用户点击微调器中的“Aqua”时,我希望屏幕变为 Aqua。

我的微调器已设置并调用

Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
mySpinner.setAdapter(new MyAdapter(this,R.layout.custom_spinner,spinnerValues));

但不确定我应该如何处理。看到许多不同的答案,但没有发现任何工作。

我知道我的开关会出现在这部分

class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    }
}

任何帮助都是appricated!!

【问题讨论】:

  • 您想根据选择更改屏幕背景吗?
  • 是的,每个微调器项目。例如,当您单击微调器并出现不同的数组项时,每个项都应该有自己的背景

标签: java android arrays string spinner


【解决方案1】:

如果您想在单击项目时从微调器数组中检索对象,您可以使用方法 onItemSelected 中的位置参数轻松完成。

例如:-

class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
   String itemSelected = spinnerValues[position];
   //Set this to a constant
}

}

现在,如果您选择 Aqua,那么代码会将变量 itemSelected 设置为 Aqua

我希望这是你需要的

【讨论】:

  • 我也会尝试一下,看看这个解决方案是否也能满足我的需求!
【解决方案2】:
final Spinner cardStatusSpinner1 = (Spinner)findViewById(R.id.text_interested);
String cardStatusString;
cardStatusSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        cardStatusString = parent.getItemAtPosition(pos).toString();
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});

【讨论】:

    【解决方案3】:
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      }
    

    将代码放在下面首先获取项目,然后您可以通过获取 aqua 或任何颜色等值来进行操作。

     spinner = (Spinner)findViewById(R.id.spinner1);
    spinner.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1,spinnerValues));
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            TextView tv = (TextView)view;
            Toast.makeText(getApplicationContext(), tv.getText().toString(), 5000).show();
            switch(tv.getText().toString()){
            case "Aqua":{
                //change  the color to aqua
                break;
            }
            case " ...":{
                //
                break;
            }
            //.... for all the option
            }
        }
    

    【讨论】:

    • 或者你可以直接从字符串contant获取字符串。为此,您必须使用 public void onItemSelected(AdapterView> parent, View view, int position, long id) { String color = spinnerValues[position];开关(颜色){
    • 谢谢!该解决方案完美运行,应该被赞成。我正在使用 public void onItemSelected(AdapterView> parent, View view, int position, long id) { String color = spinnerValues[position];开关(颜色)实现。最后一个问题。为了停止显示我正在使用的效果 mCamera.stopPreview(); mCamera.startPreview();停止显示效果,然后再次开始预览。这是推荐的方法吗?
    猜你喜欢
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 2019-02-17
    相关资源
    最近更新 更多