Android : calculating the total value of selected array items

on Saturday, October 25, 2014


What I have is two different arrays, one for the items and one for the value of the item. The spinners are connected to the array with the items so that I can select what I want from them. After I have selected two items I want it to calculate the total value of the two items selected in the spinners. You do this by matching the index between the two arrays, so item one have index 1 and the price for item one in arrayV is also index 1.


So what I need help with is the code that once I selected items in the two spinners the total value will be shown as text.



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUp();
}

private void setUp() {
array = getResources().getStringArray(R.array.currency_name);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
arrayV = getResources().getStringArray(R.array.currency_value);
adapterV = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arrayV);
adapterV.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn1 = (Spinner) findViewById(R.id.spinner1);
spn1.setAdapter(adapter);
spn1.setOnItemSelectedListener( new Listener() );
spn2 = (Spinner) findViewById(R.id.spinner2);
spn2.setAdapter(adapter);
spn2.setOnItemSelectedListener( new Listener() );
result = (TextView) findViewById(R.id.txtView5);
}

private class Listener implements OnItemSelectedListener {}

0 comments:

Post a Comment