0

我在一个屏幕上设置了一个微调器,我从另一个屏幕传递了一个字符串。我希望将该字符串设置为微调器的默认值。我查看了帖子并尝试实施它,但不知何故它不起作用。

这是我的代码:

//workRequestFetched is a private String object
workRequestFetched = extras.getString("workRequest");

workRequestSpinner = (Spinner) findViewById(R.id.workRequestSpinner);

//ServiceCall is a function call to the web service it works fine otherwise, please don't worry about it.

ArrayList<String> workRequests = ServiceCall.workRequests;



        ArrayAdapter<String> workRequestAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,
                 workRequests);

selected 是布尔值,用于检查字符串是否已通过。我已经打印出来检查执行线程是否进入 if 子句并且确实如此,所以认为问题不存在。

        if(selected){
            System.out.println("Entered selected- "+workRequestFetched);
//This is what supposed to set the spinner's default to the position of the string right? but it doesn't work!

            int spinnerPosition = workRequestAdapter.getPosition(workRequestFetched);

            workRequestSpinner.setSelection(spinnerPosition);


        }

        workRequestAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


        workRequestSpinner.setAdapter(workRequestAdapter);

谁能指出错误?谢谢!

4

1 回答 1

2

将您的代码段更改为以下

ArrayAdapter <String> workRequestAdapter = new ArrayAdapter <String> (
this,android.R.layout.simple_spinner_item,workRequests);
workRequestAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
workRequestSpinner.setAdapter(workRequestAdapter);
workRequestSpinner.setSelection(workRequestAdapter.getPosition(workRequestFetched));
于 2012-06-21T14:22:05.503 回答