Associate values to spinner - android

I want to implement in my application, certain mathematical calculation, but, it has a variable that covers age group and data, I will show in practice: 'Adequação da CB = CB obtida/CB percentile 50 * 100' . This is the formula, where the variable "CB percentile 50" has 28 different forms of being expressed, for example, from 1 to 2 years old it has the value of 16, from 2 to 3 has the value of 16.3, from 3 To 4 has the value of 16.8 and so on... How do I make the spinner check the age data and return this value to be placed in the mathematical expression?
My strings file
<string name="fxetaria">Selecione a Faixa Etária</string>
<string-array name="faixaet">
<item>1 a 1.9</item>
<item>2 a 2.9</item>
<item>3 a 3.9</item>
<item>4 a 4.9</item>
<item>5 a 5.9</item>
<item>6 a 6.9</item>
<item>7 a 7.9</item>
<item>8 a 8.9</item>
<item>9 a 9.9</item>
<item>10 a 10.9</item>
<item>11 a 11.9</item>
<item>12 a 12.9</item>
<item>13 a 13.9</item>
<item>14 a 14.9</item>
<item>15 a 15.9</item>
<item>16 a 16.9</item>
<item>17 a 17.9</item>
<item>18 a 24.9</item>
<item>25 a 29.9</item>
<item>30 a 34.9</item>
<item>35 a 39.9</item>
<item>40 a 44.9</item>
<item>45 a 49.9</item>
<item>50 a 54.9</item>
<item>55 a 59.9</item>
<item>60 a 64.9</item>
<item>65 a 69.9</item>
<item>70 a 74.9</item>
</string-array>
And my activity:
public class adeqcbh extends AppCompatActivity {
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adeqcbh);
spinner = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this, R.array.faixaet, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}

I'm thinking the best way would be to have another array with the values (the same amount as your first array).
<string-array name="faixaet_values">
<item>16</item>
<item>16.3</item>
...
<string-array>
Then, in your onItemSelected method, just get the value from the new string array, like so;
String valueString = getResources().getStringArray(R.array.faixaet_values)[position - 1];
float value;
try {
value = Float.valueOf(valueString);
} catch(NumberFormatException e) {
Log.e(TAG, "This is not a number");
}

Related

Populating Spinner from another Spinner

I have the following doubt I have to use some Arrays List in my project they are ...
List Restaurant Dishes Restaurant 1 Dishes Restaurant 2 Price Restaurant 1 Price Restaurant 2
Within my Activity I put two Spinners one that will get the Array List Restaurants and the other the I want it to appear the data of the Array list when I select the restaurant and in the end I would pick and setaria as a Text View of the price.
Just to get an idea of ​​what's being populated in Spinner
<string-array name="restaurant">
<item>McDonalds</item>
<item>KFC</item>
</string-array>
<string-array name="restaurant_McDonalds_Plate">
<item>Combo Big Mac</item>
<item>Combo Deluxe Bacon</item>
<item>Combo Club House</item>
</string-array>
<string-array name="Restaurant_McDonalds_Plate_Price">
<item>R$ 31,00</item>
<item>R$ 29,00</item>
<item>R$ 35,00</item>
</string-array>
<string-array name="Restaurant_KFC_Plate">
<item>Combo Balde de 6 peças</item>
<item>Combo Balde de 9 peças</item>
<item>Combo Balde de 12 peças</item>
</string-array>
<string-array name="Restaurant_KFC_Plate_Price">
<item>R$ 25,00</item>
<item>R$ 35,00</item>
<item>R$ 45,00</item>
</string-array>
try below code , i have explained and wrote it ..
final Spinner retaurant;
final Spinner plate;
final TextView txt_retaurant;
final TextView price;
retaurant.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String restaurantname = retaurant.getSelectedItem().toString();
txt_retaurant.setText(restaurantname);
//if McDonalds, populates plate spinner with respective
if(position == 0){
ArrayAdapter<String> adapter= new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.restaurant_McDonalds_Plate));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
plate.setAdapter(adapter);
}
//if kfc, populates plate spinner with respective
if(position == 1){
ArrayAdapter<String> adapter= new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Restaurant_KFC_Plate));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
plate.setAdapter(adapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
plate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// if McDonalds selected , it gets respective price list of plate and sets the price list to textview according to selected value in plate spinner
if(retaurant.getSelectedItemPosition() == 0){
List<String> myArrayList = Arrays.asList(getResources().getStringArray(R.array.Restaurant_McDonalds_Plate_Price));
price.setText(myArrayList.get(position));
}
// if kfc selected , it gets respective price list of plate and sets the price list to textview according to selected value in plate spinner
if(retaurant.getSelectedItemPosition() == 1){
List<String> myArrayList = Arrays.asList(getResources().getStringArray(R.array.Restaurant_KFC_Plate_Price));
price.setText(myArrayList.get(position));
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

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

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.

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>

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