eclipse 如何使 On Item Selected 不自动选择第一个条目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10132971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:01:14  来源:igfitidea点击:

How to make On Item Selected not automatically choose the first entry

javaandroideclipsespinner

提问by Sketzii

I have created a spinner which is automatically updated with appliance names when a person adds an appliance using an array adapter. I created an OnItemSelected method with the spinner so when one of the names in the spinner is selected, a new window appears. However the OnItemSelected is automatically selecting the first item on the list when the activity starts and so the user does not have a chance to actually make a selection until the new window appears.

我创建了一个微调器,当有人使用阵列适配器添加设备时,它会自动更新设备名称。I created an OnItemSelected method with the spinner so when one of the names in the spinner is selected, a new window appears. 然而,当活动开始时 OnItemSelected 会自动选择列表中的第一项,因此用户在新窗口出现之前没有机会实际进行选择。

Here is the code:

这是代码:

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    startActivity(new Intent("com.lukeorpin.theappliancekeeper.APPLIANCESELECTED"));
    }

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

Does anyone know a way in which the first item on the list wont be automatically selected?

有谁知道不会自动选择列表中的第一项的方法?

Here is the code for the rest of the spinner:

这是微调器其余部分的代码:

ArrayAdapter<String> appliancenameadapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, ApplianceNames); //Sets up an array adapter containing the values of the ApplianceNames string array
    applianceName = (Spinner) findViewById(R.id.spinner_name); //Gives the spinner in the xml layout a variable name
    applianceName.setAdapter(appliancenameadapter); //Adds the contents of the array adapter into the spinner

    applianceName.setOnItemSelectedListener(this);

采纳答案by CommonsWare

Does anyone know a way in which the first item on the list wont be automatically selected?

有谁知道不会自动选择列表中的第一项的方法?

There is alwaysa selection on Spinner, and you cannot change that.

总是一个选择Spinner,你不能改变的。

IMHO, you should not be using a Spinnerto trigger starting an activity.

恕我直言,您不应该使用 aSpinner来触发启动活动。

That being said, you can use a booleanto track whether this is the first selection event, and ignore it if it is.

话虽如此,您可以使用 aboolean来跟踪这是否是第一个选择事件,如果是,则忽略它。

回答by James Wald

If you are trying to avoid the initial call to your listener's onItemSelected()method, another option is to use post()to take advantage of the view's message queue. The first time the spinner checks for your listener it won't be set yet.

如果您试图避免对侦听器onItemSelected()方法的初始调用,另一种选择是利用post()视图的消息队列。微调器第一次检查您的侦听器时,它还不会被设置。

// Set initial selection
spinner.setSelection(position);

// Post to avoid initial invocation
spinner.post(new Runnable() {
  @Override public void run() {
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // Only called when the user changes the selection
      }

      @Override
      public void onNothingSelected(AdapterView<?> parent) {
      }
    });
  }
});

回答by karan

It worked for me,

它对我有用,

private boolean isSpinnerInitial = true;

private boolean isSpinnerInitial = true;

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

        if(isSpinnerInitial)
        {
            isSpinnerInitial = false;
        }
        else  {
            // do your work...
        }

    }

回答by Jin Thakur

Declare variable isSpinnerInitial then make Make a Selection as your default selection

声明变量 isSpinnerInitial 然后将选择作为您的默认选择

spinnertaggeview.setSelection(-1); does not make selection as -1 or everything unselected as we do in .Net or other language . So you can ignore thatline.

spinnertaggerview.setSelection(-1); 不会像我们在 .Net 或其他语言中那样将选择设为 -1 或所有未选中的内容。所以你可以忽略那条线。

testStringArrayListinside.add("Make a Selection");
ADD this line so that this is selected by default and user never selects it 

testStringArrayList = (ArrayList<String>) ClinqLinX.get("Tag");
                boolean added = false;
             testStringArrayListinside.add("Make a Selection");
                for (String s : testStringArrayList) {
                    if (s != null || s != "") {
                        String[] results = s.split(","); // split on commas

                        for (String string : results) {

                            testStringArrayListinside.add(string);
                            Toast.makeText(getContext(), string, Toast.LENGTH_SHORT).show();
                            added = true;
                        }
                    }

                }
                if (added == false) {
                    testStringArrayListinside.add("Not tagged details found");
                }

                spinnertaggeview.refreshDrawableState();

            }

            // Adapter: You need three parameters 'the context, id of the layout (it will be where the data is shown),
            // and the array that contains the data
            if (testStringArrayListinside.size() > 0) {
                adapter = new ArrayAdapter<String>(this.getContext(),android.R.layout.select_dialog_singlechoice, testStringArrayListinside);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                // Here, you set the data in your ListView
                spinnertaggeview.setAdapter(adapter);
                 isSpinnerInitial = true;

                spinnertaggeview.setSelection(-1);
                spinnertaggeview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                                               int arg2, long arg3) {
                        if (isSpinnerInitial){

                            isSpinnerInitial = false;

                            return;}
                        else{
                        TextView tv = (TextView) arg1;
                        String spinner_value = tv.getText().toString();
                        if (spinner_value.length() == 0) {
                            spinner_value = "Nothing";
                        }
                        String strusername = spinner_value;//As you are using Default String Adapter
                        Toast.makeText(getContext(), strusername, Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(spinnertaggeview.getContext(), Userdetails.class);
                        intent.putExtra("Username", strusername);
                        spinnertaggeview.getContext().startActivity(intent);}

                    }