How to get Spinner value? - android

In Android, I am trying to get the selected Spinner value with a listener.
What is the best way to get the spinner's value?

Spinner mySpinner = (Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();

The Spinner should fire an "OnItemSelected" event when something is selected:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});

Say this is your xml with spinner entries (ie. titles) and values:
<resources>
<string-array name="size_entries">
<item>Small</item>
<item>Medium</item>
<item>Large</item>
</string-array>
<string-array name="size_values">
<item>12</item>
<item>16</item>
<item>20</item>
</string-array>
</resources>
and this is your spinner:
<Spinner
android:id="#+id/size_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/size_entries" />
Then in your code to get the entries:
Spinner spinner = (Spinner) findViewById(R.id.size_spinner);
String size = spinner.getSelectedItem().toString(); // Small, Medium, Large
and to get the values:
int spinner_pos = spinner.getSelectedItemPosition();
String[] size_values = getResources().getStringArray(R.array.size_values);
int size = Integer.valueOf(size_values[spinner_pos]); // 12, 16, 20

Yes, you can register a listener via setOnItemSelectedListener(), as is demonstrated here.

View view =(View) getActivity().findViewById(controlId);
Spinner spinner = (Spinner)view.findViewById(R.id.spinner1);
String valToSet = spinner.getSelectedItem().toString();

If you already know the item is a String, I prefer:
String itemText = (String) mySpinner.getSelectedItem();
Calling toString() on an Object that you know is a String seems like a more roundabout path than just casting the Object to String.

add setOnItemSelectedListener to spinner reference and get the data like that`
mSizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
selectedSize=adapterView.getItemAtPosition(position).toString();

Related

Spinner1 populate from array, then select second array for spinner2 based on 1st spinner selection

Okay spinner 1 uses Manu_array
spinner 2 uses Manu 1xsd, Manu 2xrsd or 3x4rsd
all I need for now is to be able to populate the second spinner, after selecting the a value from the Manu_array in spinner 1.
<!--for spinner 1-->
<string-array name="Manu_array">
<item>Manu 1xsd</item>
<item>Manu 2xrsd</item>
<item>Manu 3x4rsd</item>
</string-array>
<!--for spinner 2-->
<string-array name="Manu 1xsd">
<item>a1</item>
<item>a2</item>
<item>a3</item>
<item>a4</item>
</string-array>
<string-array name="Manu 2xrsd">
<item>bg 1</item>
<item>bg 2</item>
</string-array>
<string-array name="Manu 3x4rsd">
<item>z1</item>
<item>z2</item>
<item>zd4</item>
<item>xs5</item>
<item>fg34</item>
</string-array>
My java file code:
final Spinner[] spinner1 = {(Spinner) findViewById(R.id.spinner1)};
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Manu_array, R.layout.textview);
spinner1[0].setAdapter(adapter);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1[0].setAdapter(adapter);
spinner1[0].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
// storing string resources into Array
Manu= getResources().getStringArray(R.array.Manu_array);
Toast.makeText(getBaseContext(), "You have selected : " + Manu[index],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
});
I can process code that will allow the spinner to populate with the array data, but I cant get the second spinner to populate based on the selection of the first spinner, surely its simple if equals value use array.
The spinner above shows the values for the Manu_array, the next spinner has to take the value selected and populate with the appropriate array values, but all I have managed to do is generate a second spinner with the array values I have manual typed in, not selected.
if(Manu.equals("Manu 1xsd")){spinner2 languages = getResources().getStringArray(R.array.Manu 1xsd_array)}else......
Any suggestions where I may look for examples please looked at a few on here and confused me something chronic.
Set the second spinner adapter when an item from the first spinner is clicked
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int arrayId = 0;
switch(position) {//get array id according to selected position
case 0:
arrayId = R.array.Manu_1xsd;
break;
case 1:
arrayId = R.array.Manu_2xrsd;
break;
case 2:
arrayId = R.array.Manu_3x4rsd;
break;
};
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, arrayId, R.layout.textview);
spinner2.setAdapter(adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

Spinner Value not working

I am running into some trouble with my spinners. When every I select a value from them the spinner only takes the first value in the list.
For example I have a spinner with vales 1,2,3,4,5. When I select 4 the value taken from the spinner 1, this is the same if I select any of the other values.
Am I implementing the Spinner wrong? Or taking the values from the spinner wrong?
Spinner
<Spinner
android:id="#+id/heatSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/heat_title"
android:entries="#array/heat"/>
Array/Heat
<string-array name="heat">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>
Taking Values from Spinner
Spinner heat = (Spinner) dialog.findViewById(R.id.heatSpinner);
final String heatValue = heat.getSelectedItem().toString();
final int finalHeat = Integer.parseInt(heatValue);
I had a similar problem, I dunno if this way is the best way to do it but it worked for me.
You need to set a setOnItemSelectedListener for each spinner, inside this you will be then able to set the value of your variable to the items of the spinner.
So your spinner might look something like this, again this may not be the nicest looking approach but it worked for me anyway, hope it helps
final Spinner heat = (Spinner) dialog.findViewById(R.id.heatSpinner);
heat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
final String heatValue = heat.getSelectedItem().toString();
int finalHeat = Integer.parseInt(heatValue);
db1.updateHeat(finalHeat, recipe_number);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Let's look at the 4ยบ block of code in your question.
Are you sure that
int servingSize = servings.getInt(0);
Is getting the value that you want?
And in
seekBar.setProgress(size);
what is this 'size' parameter? Are you sure that you are passing the right value to the setProgress() method? I didn't see this 'size' variable in your code.

How to make spinner return an id in android?

I have made a Spinner in Android:
category = (Spinner) findViewById(R.id.ev_category);
category.setOnItemSelectedListener(new MyOnItemSelectedListener());
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.category_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
category.setAdapter(adapter);
The resource for the spinner is:
<string name="category">Choose a category</string>
<string-array name="category_list">
<item>Food</item>
<item>Art And Performance</item>
<item>Classes</item>
<item>Others</item>
</string-array>
See, I am getting my selected Spinner text by implementing the following method. It is returning my selected item text in my Spinner.
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
ev_category=parent.getItemAtPosition(pos).toString();
}
I want the below functionality. If the first item in my spinner is selected I want to get the values as "1" and if the next item in spinner is selected i want to get value as "2".
How can I accomplish this task?
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
pos here is position isn't it?, add 1 to it, if it returns 0 for first item.
if you want string, then convert integer to string using Integer.toString()
in any method u can call getItemPosition = spinner.getSelectedItemPosition();
and you can use (switch) it will return you 0, 1, 2.

How to save the spinner selected value in android?

I am trying to save spinner selected value, but i am getting like below i shown when i retrieve the details. Anybody know what is the problem.
Spinner:android.widget.Spinner#43e807a0
Have you used the getSelectedItem() inside setOnSelectedListner? If not, do as shown below:
mPres_doctor.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapter, View view,
int position, long id) {
String pres_doctor = mPres_doctor.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
The result is displaying as an object value, usually I follow the below method to get the spinner values:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner, android.R.layout.spinner_layout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
strings.xml
<string-array name="spinner">
<item>Dev</item>
<item>Stieve</item>
<item>John</item>
<item>Britto</item>
</string-array>

spinner adding string array on item selection how can get item related value in android

i am developing one spinner this spinner i am string array
spinner = (Spinner)this.findViewById(R.id.mlg);
final CharSequence[] itemArray =getResources().getTextArray(R.array.RectBeam);
final List<CharSequence> itemList =new ArrayList<CharSequence>(Arrays.asList(itemArray));
adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,itemList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
............................
<string-array name="RectBeam">
<item value="3000000" > Steel</item></string-array>
this is the spinner related string array i am get the spinner item i am using parent.getItemAtPosition(pos).toString(),done my problem is particular item value how can get
example : steel----------->3000000
I am not sure either Spinner allow that attribute value in XML String or not but your problem can be solved like this.
Create two arrays in your array.xml file like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="items">
<item>Nokia 1200</item>
<item>Nokia 1600</item>
<item>Nokia 5130</item>
</string-array>
<string-array name="values">
<item>1000</item>
<item>2000</item>
<item>5000</item>
</string-array>
</resources>
Now load first array in your adapter and store the second one in other Array to hold values of items as:
String items [] = getResources().getStringArray(R.array.items);
String values [] = getResources().getStringArray(R.array.values);
And you can simply get the respective item name and value in your onItemSelected() method like this:
String item = parent.getItemAtPosition(pos).toString();
String value = values [pos];

Categories

Resources