简体   繁体   English

如何在 Android 中为 Spinner 设置所选项目

[英]How to set selected item for Spinner in Android

In an Android app, I have a SpinnerAdapter that I am using to display expiration years of credit cards.在 Android 应用程序中,我有一个SpinnerAdapter ,用于显示信用卡的到期年限。 My variable expirationYears contains an array of Strings with years from "2020" to "2029" .我的变量expirationYears包含一个字符串数组,年份从"2020""2029" It was declared as String[] expirationYears = new String[10];它被声明为String[] expirationYears = new String[10]; and then with a function I set the ten years as Strings, from current year and then the nine subsequent years after that for a total of ten elements in the array.然后使用一个函数,我将十年设置为字符串,从当前年份开始,然后是随后的九年,数组中总共有十个元素。 When I run it, this is what I see:当我运行它时,我看到的是:

在此处输入图片说明

Everything is perfect so far.到目前为止一切都很完美。 The only problem is that when as you see in the image above, 2020 is the year selected by default.唯一的问题是,当您在上图中看到时, 2020是默认选择的年份。 That is the first element in the expirationYears array.这是expirationYears数组中的第一个元素。 But I want to set 2021 as default (the second element of the array).但我想将2021设置为默认值(数组的第二个元素)。 This is what my code has:这是我的代码所具有的:

Spinner spinnerYearCreditCardPayment;
SpinnerAdapter creditCardYearAdapter = new SpinnerAdapter(Payment.this, expirationYears);
creditCardYearAdapter
    .setDropDownViewResource(android.R.layout.spinner_dropdown_item);
spinnerYearCreditCardPayment.setAdapter(creditCardYearAdapter);
spinnerYearCreditCardPayment
    .setOnItemSelectedListener(new OnItemSelectedListener() {
        ..........
    });

I was expecting to have some property available to define which element I want to show by default.我期待有一些属性可用于定义我想要默认显示的元素。 Something like creditCardYearAdapter.setSelectedItem(INDEX_NUMBER) .类似于creditCardYearAdapter.setSelectedItem(INDEX_NUMBER) But that property does not exist.但该属性不存在。 Do you know how I could set the default selected item so that it is not the first element of the array (2020) but the second (2021)?您知道我如何设置默认选定项,使其不是数组的第一个元素 (2020) 而是第二个元素 (2021)? Thank you.谢谢你。

UPDATE 1:更新1:

Following The_Martian's answer, this was what I did:按照 The_Martian 的回答,这就是我所做的:

spinnerYearCreditCardPayment
    .setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
           arg0.setSelection(1);
           cardYear = (String) spinnerYearCreditCardPayment
               .getSelectedItem();
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

Now I correctly see the second element of the array ("2021") as the default selection.现在我正确地将数组的第二个元素(“2021”)视为默认选择。

The problem is that when I use arg0.setSelection(1);问题是当我使用arg0.setSelection(1); , I choose another year and it does not respond. ,我选择了另一年,它没有响应。 The selected year remains "2021" even thought I am trying to change the year.即使我想改变年份,所选年份仍然是“2021”。

You can do similar to the following.您可以执行类似于以下操作。 I am just giving you an idea here.我只是在这里给你一个想法。

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                 parent.setSelection(position + 1);  
            }

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

            }
        });

Try this snippet试试这个片段

  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
    {
       public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) 
         {

               int seleccion=parent.getSelectedItemPosition();
                spinner.setSelection(position)
        });
    }

You can select the spinner item based on value instead of position.您可以根据值而不是位置来选择微调项。 Can refer the example below-可以参考下面的例子-

String value = "to be selected value";
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.select_state, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
if (value != null) {
    int spinnerPosition = adapter.getPosition(value);
    mSpinner.setSelection(spinnerPosition);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM