Wondering if i can get some help with a problem i’m having? I have searched for the answer on here, and although i found a few relevant topics i’m still really struggling…
Below you can see that i’m using a Spinner asking the user to select either Large, Med or Small.
The selection is then issued to a TextView.
What i now want to do is assign a value to the 3 selections, Large = 6, Med = 4, Small =2.
Next there will be a EditText box for the user to add their own value.
Then i want to put the two values into a calculation (say * them in this example) putting the answer into a TextView.
Any help here would be great.
Many Thanks
Will
String[] items = { "Large", "Med", "Small" };
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.plugfan);
Factor = (TextView) findViewById(R.id.Factor);
Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(
this,
android.R.layout.simple_spinner_item,
items);
aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
Factor.setText(items[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
Factor.setText("");
}
A quick way to solve this (not the smartest one) is to have a paralell array:
In the method onItmeSelected you can setText using the itemValue, instead of items.
If you want a more OO-solution a new class that returns a list of Strings to use as a model for your spinner might be a beter solution (an enumeration might solve it too).