Spinner Value not working - android

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.

Related

How to display the distance and time travelled between two points using spinners?

I want to create an app by selecting town-a from one spinner and then town-b from the other spinner. I then want to display the distance between those towns as well as the time it would take to get there from the selected towns.
I have found something like this:
var dist = [
['Town B', [], []],
['Town C', [67], ['1h00m']],
['Town D', [282,251], ['11h10m','10h00m']],
['Town E', [243,210,41], ['9h45m','8h25m','1h40m']],
Now this is obviously for HTML dropdown boxes and then calculates from the above file (dist.js)
Now I would like to know how to convert that to using two spinners. I think I have the basic idea but not sure how to implement. What I am thinking is when spinner 1 is selected and then spinner 2 then it needs to say that spinner 1 = spinner 2 and the distance is 67km and time to get the is 1h00m.
I don't have code for this cause I haven't tried it yet because I am not sure where to start. So am hoping someone would help me?
EDIT
This is what I have sofar:
public class MainActivity extends Activity implements OnItemSelectedListener {
private Spinner startloc;
private Spinner enddes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startloc = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter_start = ArrayAdapter
.createFromResource(this, R.array.start_location,
android.R.layout.simple_spinner_item);
adapter_start.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
startloc.setAdapter(adapter_start);
startloc.setOnItemSelectedListener(this);
enddes = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter_end = ArrayAdapter
.createFromResource(this, R.array.end_location,
android.R.layout.simple_spinner_item);
adapter_end.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
enddes.setAdapter(adapter_end);
enddes.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
And this:
<string-array name="start_location">
<item>Town a</item>
<item>Town b</item>
<item>Town c</item>
</string-array>
<string-array name="end_location">
<item>Town a</item>
<item>Town b</item>
<item>Town c</item>
</string-array>
<string-array name="distance">
<item>0</item>
<item>67</item>
<item>282</item>
<item>251</item>
</string-array>
<string-array name="time">
<item>0m</item>
<item>2h40m</item>
<item>11h10m</item>
<item>10h00m</item>
So basically this means: Town a to Town a is 0 and time is 0, then Town a to Town b is 67km and time is 2h40m, Town a to town c is 282km 11h10m and finally Town b to town c 251km and time is 10h00m. (and needs to obviously work in reverse of list order as well)
Now how do I do this?
Screen shot:
I think you may be hung up on how the data that populates a spinner works in Android.
A Spinner in Android uses an Adapter to fill the Spinner with backing data. The data could be a collection of custom objects, Strings, Integers, etc... The Spinner can also register a listener so that when an item is selected from the Spinner a callback is made that indicates which item was selected.
A very simple implementation would be to use arrays of Strings populate the Spinners. When a selection is made for Spinner1, you'd store that value somewhere. When a selection is made for Spinner2 you'd store that value and check to see that a selection had been made for Spinner1. If a selection had been made for each spinner, then you'd be able to look up the distance / time for each selection. A very simple way to do this would be to use if-else statements:
if (spinnerOne.equals("Denver")) {
if (spinnerTwo.equals("New York") {
//Print out that distance equals 1280 miles
} else if (spinnerTwo.equals("San Fransisco") {
//Etc...
}
} else if (spinnerOne.equals("New York") {
//Etc...
}
The implementation I describe above is a super simple way of doing it and would work but is mainly just for illustration. There are probably better ways of structuring the data.

Android - Spinners

I have a question and know this has been asked a million times, I am fairly new to Android Programming. So I am kinda boggled.
I have created two parallel arrays - containing phrases in two languages.As the spinner changes it value(from 1st array) - the textview should be updated to reflect the translated phrase (in the second array).
The two arrays are created in string.xml, similar to below and the spinner gets it value from there.
<string-array name="array_name">
<item>phrase1</item>
<item>phrase2</item>
<item>phrase3</item>
<item>phrase4</item>
</string-array>
Im not sure how to go about doing this or if this is a viable way of doing it.... but boggled I remain....
Can someone please help?
You should set OnItemSelectedListener to spinner and change the text
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textView.setText(getResources().getTextArray(R.array.array_name)[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

update spinner array depend on other's selection

Spinner spinner, spinner2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box_tuts);
spinner = (Spinner)findViewById(R.id.spinner1);
spinner2 = (Spinner)findViewById(R.id.spinner2);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> v, View arg1, int arg2,
long arg3) {
switch(v.getId()){
case R.id.spinner1:
if(spinner.getSelectedItemPosition() == 1){
//what goes here??
break;
}
}
}
Like my first spinner lists are countries : USA, JAPAN etc.. so after USA has been selected for example, my second spinner would able to get a list of USA's state array..
I use my newbie logic to try to think this way : set the 2nd spinner to View.GONE, then if lets say USA is selected, show the spinner2 which it alone carry the states.. but if I have 60 countries, then I must have 60 spinner, that's funny so help me :D
Rather than creating a Spinner for each country instance, keep the same Spinner, and replace the data stored in it when a value is selected from your first Spinner with
ArrayAdapter.createFromResource() and Spinner.setAdapter(). Here is an example using those two calls.
Rather than hiding your second Spinner before an option is selected in the first, consider having a default selection in the first Spinner, and populating the second with the matching data. In your example, USA would be a good default selection.

Populating spinner directly in the layout xml

Is it possible to populate the options of a Spinner right in the layout xml? This page suggests I should use an ArrayAdapter? It seems awkward not being able to do it..
I'm not sure about this, but give it a shot.
In your strings.xml define:
<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item Two</item>
<item>Array Item Three</item>
</string-array>
In your layout:
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:entries="#array/array_name"
/>
I've heard this doesn't always work on the designer, but it compiles fine.
Define this in your String.xml file and name the array what you want, such as "Weight"
<string-array name="Weight">
<item>Kg</item>
<item>Gram</item>
<item>Tons</item>
</string-array>
and this code in your layout.xml
<Spinner
android:id="#+id/fromspin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/Weight"
/>
In your java file, getActivity is used in fragment; if you write that code in activity, then remove getActivity.
a = (Spinner) findViewById(R.id.fromspin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
R.array.weight, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
a.setAdapter(adapter);
a.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (a.getSelectedItem().toString().trim().equals("Kilogram")) {
if (!b.getText().toString().isEmpty()) {
float value1 = Float.parseFloat(b.getText().toString());
float kg = value1;
c.setText(Float.toString(kg));
float gram = value1 * 1000;
d.setText(Float.toString(gram));
float carat = value1 * 5000;
e.setText(Float.toString(carat));
float ton = value1 / 908;
f.setText(Float.toString(ton));
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
// Inflate the layout for this fragment
return v;
}
In regards to the first comment: If you do this you will get an error(in Android Studio). This is in regards to it being out of the Android namespace. If you don't know how to fix this error, check the example out below. Hope this helps!
Example -Before :
<string-array name="roomSize">
<item>Small(0-4)</item>
<item>Medium(4-8)</item>
<item>Large(9+)</item>
</string-array>
Example - After:
<string-array android:name="roomSize">
<item>Small(0-4)</item>
<item>Medium(4-8)</item>
<item>Large(9+)</item>
</string-array>

How to get Spinner value?

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

Categories

Resources