Get spinner selected items text?
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
Get Text of Selected Spinner in android
To get the text of selected item use spinner.getAdapter().getItem(position)
spino = dialog.findViewById(R.id.language_spinner);
if (spino != null) {
spino.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item_position = String.valueOf(position);
int itemposition = Integer.parseInt(item_position);
String selected = String.valueOf(spino.getAdapter().getItem(position));
Log.e("selected position",""+itemposition);
Log.e("selected Text",selected);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
else{
Log.e("Selected item :","NULL:");
}
How to get Spinner selected item value to string?
Try this:
String text = mySpinner.getSelectedItem().toString();
Like this you can get value for different Spinners.
How to get selected item from spinner in android
Try this!!!
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String yourName=spiner.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
If code above not help you. Try this!!
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
Get spinner selected item and passing data
I think the issue is this line if(selected=="Japan")
In Java you will need to use equals
instead of the ==
operator.
The ==
checks if the two pointers are pointing to the same memory location.Equals
will compare their content.
How to get item selected in Spinner to use it as string?
you might need to implement the selection handle (ItemSelected
) like in this example from https://developer.xamarin.com/guides/android/user_interface/spinner/:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "Main" layout resource
SetContentView (Resource.Layout.Main);
Spinner spinner = FindViewById<Spinner> (Resource.Id.spinner);
spinner.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs> (spinner_ItemSelected);
var adapter = ArrayAdapter.CreateFromResource (
this, Resource.Array.planets_array, Android.Resource.Layout.SimpleSpinnerItem);
adapter.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = adapter;
}
and here is the handle, the index to selected item appears as e.Position
here.
private void spinner_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
string toast = string.Format ("The planet is {0}", spinner.GetItemAtPosition (e.Position));
Toast.MakeText (this, toast, ToastLength.Long).Show ();
}
How to get the selected spinner item to a string?
@Jeff.H is right, but you need the answer in C#. So, you have to do something like this (I tried it):
Spinner spinner = (Spinner)sender;
string selectedItem = spinner.SelectedItem.ToString();
Hope this help you.
How to get spinner's selected item's view
Instead of using,
spinner.getSelectedItem()
which return null, so use this :
((TextView)(spinner.getSelectedView().findViewById(R.id.title))).getText()
I think it work for you.
In this case, you will be having whole selected view also, which is you requirement i think by:
spinner.getSelectedView()
How to get spinner value after item select in spinner
If you don't want to use a custom adapter then fetch selected spinner position and using that position fetch your State
model
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// int position = spinnerState.getSelectedItemPosition();
if(position >= 1){
AllStates allStates = stateList.get(position - 1)
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Now you can access id
from allStates
model
How can I discriminate the selected item of a Spinner by its text when using multilanguage items in Android?
I had the same problem in the past and here is how I solved it.
Edit your strings.xml files by adding a string resource name for each items in your array, for example:
Default strings.xml
<string name="length">length</string>
<string name="weight">weight</string>
<string name="temperature">temperature</string>
<string-array name="spinner_items">
<item>@string/length</item>
<item>@string/weight</item>
<item>@string/temperature</item>
</string-array>
Italian strings.xml
<string name="length">lunghezza</string>
<string name="weight">peso</string>
<string name="temperature">temperatura</string>
<string-array name="spinner_items">
<item>@string/length</item>
<item>@string/weight</item>
<item>@string/temperature</item>
</string-array>
So in your code, you'll have:
when(spinner.getItemAtPosition(position).toString()) {
getString(R.string.length) -> actionLength()
getString(R.string.weight) -> actionWeight()
getString(R.string.temperature) -> actionTemperature()
}
I hope I was helpful!
Related Topics
Is This the Right Way to Clean-Up Fragment Back Stack When Leaving a Deeply Nested Stack
How to Set Up Android Emulator Proxy Settings
How to Use 3G Connection in Android Application Instead of Wi-Fi
Is There Any Reason to Use the Support.V4 Library in Android
Android Gradle Could Not Reserve Enough Space for Object Heap
Recognizing Multiple Keywords Using Pocketsphinx
Why Are There Two Build.Gradle Files in an Android Studio Project
Why Do I Get "Unresolved Reference" Error for My View's Name/Id When I Type It in Kotlin
Emulate Mifare Card with Android 4.4
Android Studio Error "Installed Build Tools Revision 31.0.0 Is Corrupted"
Starting an Activity from a Service After Home Button Pressed Without the 5 Seconds Delay
Android Emulator Not Able to Access the Internet
Android - Open Resource from @Drawable String
How to Record Phone Calls in Android
Android: Activity Getting Destroyed After Calling Camera Intent
Get the Id of a Drawable in Imageview