Android 如何将 Spinner 选定的项目值转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10331854/
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-08-20 03:19:54  来源:igfitidea点击:

How to get Spinner selected item value to string?

androidandroid-spinner

提问by Alan Lai

I have 5 Spinners. In order to make it summary to this.

我有 5 个微调器。为了使它总结到这一点。

This is Spinner in xml

这是 xml 中的 Spinner

<Spinner
            android:id="@+id/text_interested"
            android:layout_span="2"
            android:layout_width="wrap_content"
            android:layout_height="60px"
            android:entries="@array/interestedarrays"
            android:prompt="@string/interestedprompt" />

This is Spinner in Java

这是 Java 中的 Spinner

submitbtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
interested.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        public void onItemSelected(
                                AdapterView<?> adapterView, View view,
                                int i, long l) {
                            interesting = interested.getItemAtPosition(i).toString();
                        }

                        public void onNothingSelected(
                                AdapterView<?> adapterView) {

                        }
                    });
    }
});

Explanation here:

这里的解释:

The page got a button. This button will read the data from spinner when pressed. I checked the output with this

该页面有一个按钮。按下此按钮时,将从微调器读取数据。我用这个检查了输出

System.out.println(interested.getItemAtPosition(i).toString());

It gave me nothing not even null.

它没有给我任何东西,甚至没有。

How to retrieve the value and to string it?

如何检索值并将其字符串化?

回答by Bhavin

Try this:

尝试这个:

String text = mySpinner.getSelectedItem().toString();

Like this you can get value for different Spinners.

像这样,您可以获得不同 Spinner 的价值。

回答by santoshpatmca

String Text = mySpinner.getSelectedItem().toString();

OR

或者

mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Object item = parent.getItemAtPosition(position);
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

回答by Lalit Poptani

You can get the selected item from Spinner by using,

您可以使用以下方法从 Spinner 获取所选项目,

interested.getSelectedItem().toString();

回答by sush

try this

尝试这个

 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

        }
    });

 final Button saveBtn = (Button) findViewById(R.id.save_button);
    saveBtn .setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            System.out.println("Selected cardStatusString : " + cardStatusString ); //this will print the result
        }
    });

回答by Mduarth

If your Spinner was populated by SQLite cursor, then the solution is:

如果您的 Spinner 是由 SQLite 游标填充的,那么解决方案是:

Spinner mySpin = (Spinner) findViewById(R.id.myspin);
mySpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
           SQLiteCursor item = (SQLiteCursor) parent.getItemAtPosition(position);
           String value = String.valueOf(item.getString(0));
           Toast.makeText(getApplicationContext(), "The option is:" + value , Toast.LENGTH_SHORT).show(); 
 }

PS: In item.getString(0)-> 0 is the index of column on cursor that you want to get.

PS:In item.getString(0)-> 0 是您想要获取的游标上列的索引。

回答by Max

In addition to the suggested,

除了推荐的,

String Text = mySpinner.getSelectedItem().toString();

You can do,

你可以做,

String Text = String.valueOf(mySpinner.getSelectedItem());

回答by Westy92

Get the selected item with Kotlin:

使用 Kotlin 获取所选项目:

spinner.selectedItem.toString()

回答by Andy

When you choose any value from spinner, then you get selected value,

当您从微调器中选择任何值时,您将获得选定的值,

interested.getSelectedItem().toString();

回答by ngesh

I think you want the selected item of the spinner when button is clicked..

我认为您需要单击按钮时微调器的选定项目..

Try getSelectedItem():

尝试getSelectedItem()

spinner.getSelectedItem()

回答by Deepak Vardhan

The best way to do this is :-

最好的方法是:-

String selectedItem = spinner.getSelectedItem().toString();

you can refer the docs here : Spinners

你可以在这里参考文档:Spinners