如何让Spinner选择项值为string?

时间:2021-04-16 20:52:59

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

我有5纺纱。为了让它总结。

This is Spinner in xml

这是xml中的旋转器

<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中的旋转器

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

页面上有一个按钮。当按下此按钮时,该按钮将从spinner读取数据。我用这个来检查输出

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

It gave me nothing not even null.

它什么都没有,甚至是零。

How to retrieve the value and to string it?

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

12 个解决方案

#1


138  

Try this:

试试这个:

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

Like this you can get value for different Spinners.

像这样,你可以为不同的纺纱器获得价值。

#2


14  

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) {
    }
});

#3


7  

You can get the selected item from Spinner by using,

您可以使用,

interested.getSelectedItem().toString();

#4


4  

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

#5


4  

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

如果您的旋转器中填充了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.

在item.getString(0) ->中,是你想要得到的光标上列的索引。

#6


2  

In addition to the suggested,

除了建议之外,

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

You can do,

你可以做,

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

#7


1  

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

当你从spinner中选择任何值时,你会得到选择的值,

interested.getSelectedItem().toString();

#8


1  

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

我想你在按下按钮时要选择旋转器的选项。

Try getSelectedItem():

尝试getSelectedItem():

spinner.getSelectedItem()

#9


0  

By implementing the SpinnerAdapter for your adapter object i use interested.getItem(i).toString()

通过为您的适配器对象实现SpinnerAdapter,我使用了interested.getItem(i).toString()

#10


0  

The best way to do this is :-

最好的方法是:-。

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

you can refer the docs here : Spinners

您可以参考这里的文档:Spinners

#11


0  

try this

试试这个

sp1 = String.valueOf(spinner.getSelectedItem());

#12


0  

Since the latest language for Android Development is Kotlin. Here is, how we do it in Kotlin using Anonymous object.

因为Android开发的最新语言是Kotlin。这里,我们如何在Kotlin中使用匿名对象。

spinnerName?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
    override fun onNothingSelected(parent: AdapterView<*>?) {
       println("Nothing Selected")
    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
       val selectedString = yourList[position]
    }

}

#1


138  

Try this:

试试这个:

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

Like this you can get value for different Spinners.

像这样,你可以为不同的纺纱器获得价值。

#2


14  

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) {
    }
});

#3


7  

You can get the selected item from Spinner by using,

您可以使用,

interested.getSelectedItem().toString();

#4


4  

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

#5


4  

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

如果您的旋转器中填充了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.

在item.getString(0) ->中,是你想要得到的光标上列的索引。

#6


2  

In addition to the suggested,

除了建议之外,

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

You can do,

你可以做,

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

#7


1  

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

当你从spinner中选择任何值时,你会得到选择的值,

interested.getSelectedItem().toString();

#8


1  

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

我想你在按下按钮时要选择旋转器的选项。

Try getSelectedItem():

尝试getSelectedItem():

spinner.getSelectedItem()

#9


0  

By implementing the SpinnerAdapter for your adapter object i use interested.getItem(i).toString()

通过为您的适配器对象实现SpinnerAdapter,我使用了interested.getItem(i).toString()

#10


0  

The best way to do this is :-

最好的方法是:-。

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

you can refer the docs here : Spinners

您可以参考这里的文档:Spinners

#11


0  

try this

试试这个

sp1 = String.valueOf(spinner.getSelectedItem());

#12


0  

Since the latest language for Android Development is Kotlin. Here is, how we do it in Kotlin using Anonymous object.

因为Android开发的最新语言是Kotlin。这里,我们如何在Kotlin中使用匿名对象。

spinnerName?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
    override fun onNothingSelected(parent: AdapterView<*>?) {
       println("Nothing Selected")
    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
       val selectedString = yourList[position]
    }

}