如何获得 Spinner 选定的项值字符串?

我有5个纺纱机。为了使它总结这一点。

这是 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());

它什么都没给我,甚至不是无效的。

如何检索值和字符串?

266406 次浏览

You can get the selected item from Spinner by using,

interested.getSelectedItem().toString();

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

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

Try getSelectedItem():

spinner.getSelectedItem()

Try this:

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

Like this you can get value for different Spinners.

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

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

interested.getSelectedItem().toString();

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

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.

In addition to the suggested,

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

You can do,

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

The best way to do this is :-

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

you can refer the docs here : Spinners

try this

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

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

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]
}


}

Get the selected item with Kotlin:

spinner.selectedItem.toString()
    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) {


}
});