15

我创建了一个微调器,当有人使用阵列适配器添加设备时,它会自动更新设备名称。我使用旋转器创建了一个onIe选择方法,因此选择旋转器中的一个名称时,将出现一个新窗口。但是,当活动开始时,OnItemSelected 会自动选择列表中的第一个项目,因此在新窗口出现之前,用户没有机会实际进行选择。

这是代码:

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

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

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

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);
4

4 回答 4

24

如果您试图避免对侦听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) {
      }
    });
  }
});
于 2013-02-19T17:07:20.270 回答
9

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

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

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

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

于 2012-04-12T23:07:20.313 回答
8

它对我有用,

private boolean isSpinnerInitial = true;

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

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

    }
于 2015-01-21T08:37:52.583 回答
0

声明变量 isSpinnerInitial 然后将 Make a Selection 作为默认选择

spinnertaggeview.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);}

                    }
于 2015-08-24T21:51:03.763 回答