如何将Spinner选中的项值变为字符串?

时间:2012-04-26 10:46:55

标签: android android-spinner

我有5个Spinners。为了对此进行总结。

这是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" />

这是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) {

                        }
                    });
    }
});

这里的解释:

页面上有一个按钮。按下此按钮将从微调器读取数据。我用这个

检查了输出
System.out.println(interested.getItemAtPosition(i).toString());

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

如何检索值并将其串起来?

14 个答案:

答案 0 :(得分:175)

试试这个:

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

像这样,你可以获得不同的Spinners的价值。

答案 1 :(得分:20)

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

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

答案 2 :(得分:9)

您可以使用

从Spinner获取所选项目
interested.getSelectedItem().toString();

答案 3 :(得分:5)

试试这个

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

答案 4 :(得分:5)

如果您的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:item.getString(0) - &gt; 0是您要获取的光标列的索引。

答案 5 :(得分:4)

除了建议,

public class LifeCycleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    Toast.makeText(LifeCycleActivity.this,"ON CREATE", Toast.LENGTH_SHORT).show(); 
} 
@Override
protected void onStart() {
   // TODO Auto-generated method stub
   super.onStart(); 
    Toast.makeText(LifeCycleActivity.this,"ON START", Toast.LENGTH_SHORT).show();
} 
@Override
protected void onResume() {
   // TODO Auto-generated method stub
   super.onResume(); 
    Toast.makeText(LifeCycleActivity.this,"ON RESUME", Toast.LENGTH_SHORT).show(); 
} 
@Override
protected void onPause() {
   // TODO Auto-generated method stub
   super.onPause(); 
    Toast.makeText(LifeCycleActivity.this,"ON PAUSE", Toast.LENGTH_SHORT).show(); 
} 
@Override
protected void onRestart() {
   // TODO Auto-generated method stub
   super.onRestart(); 
    Toast.makeText(LifeCycleActivity.this,"ON RESTART", Toast.LENGTH_SHORT).show(); 
} 
@Override
protected void onStop() {
   // TODO Auto-generated method stub
   super.onStop(); 
    Toast.makeText(LifeCycleActivity.this,"ON STOP", Toast.LENGTH_SHORT).show(); 
} 
@Override
protected void onDestroy() {
   // TODO Auto-generated method stub
   super.onDestroy(); 
    Toast.makeText(LifeCycleActivity.this,"ON DESTROY", Toast.LENGTH_SHORT).show();
} 
}  

你可以,

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

答案 6 :(得分:2)

我认为你想要在点击按钮时选择微调器的项目。

尝试getSelectedItem()

spinner.getSelectedItem()

答案 7 :(得分:2)

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

interested.getSelectedItem().toString();

答案 8 :(得分:2)

使用Kotlin获取所选项目:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:b="urn:swift:xsd:setr.010.001.03" exclude-result-prefixes="b">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <data>
            <messageDataApps>
                <field>
                    <fieldName>SENDER.STP</fieldName>
                    <value>
                        <xsl:value-of select="b:WBIFNMsg/b:AppMsg/b:Document/b:SbcptOrdrV03/b:MsgId/b:id"/>                                    
                    </value>
                </field>
            </messageDataApps>                                                                                                          
        </data>
    </xsl:template>
</xsl:stylesheet>

答案 9 :(得分:1)

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

答案 10 :(得分:1)

最好的方法是: -

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

您可以在此处参考文档:Spinners

答案 11 :(得分:1)

试试这个

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

答案 12 :(得分:1)

由于Android开发的最新语言是Kotlin。这是我们如何使用Anonymous对象在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]
    }

}

答案 13 :(得分:0)

    spinnerType = (AppCompatSpinner) findViewById(R.id.account_type);
    spinnerType.setPrompt("Select Type");

    spinnerType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            TypeItem clickedItem = (TypeItem) parent.getItemAtPosition(position);
            String TypeName = clickedItem.getTypeName();
            Toast.makeText(AddAccount.this, TypeName + " selected", Toast.LENGTH_SHORT).show();
        }

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

        }
    });