2

I have set a Array Adapter for my Spinner to display a drop-down list of Grades (A+, A, ...etc). All I need is when the user selects, A+, myApp should understand it as float value 10, so that the Value of A+ (=10) can be used in Formula that needs integer to calculate the overall grade. for eg, if user select B+, my APP should understand as 8.5, which can be used in the formula.

Basically, I want the USERS to see the Grades on the spinners but I need the Actual values of it for Calculation.

Can I make use of POSITION of Items on the Spinners? Kindly show an example how to retrieve it.

My String resource:

<?xml version="1.0" encoding="utf-8"?>
      <resources>
         </string-array>
          <string-array name="grade_list">
           <item>A+</item>
           <item>A</item>
          <item>A-</item>
          <item>B+</item>
          <item>B</item>
          <item>B-</item>
          <item>C+</item>
          <item>C</item>
          <item>C-</item>
          <item>D</item>
          <item>E</item>
          <item>U</item>
      </resources>

MY JAVA CODE

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class CSEsem1 extends Activity implements OnClickListener{

Spinner cs1spin1;
Spinner cs1spin2;
Spinner cs1spin3;
Spinner cs1spin4;
Spinner cs1spin5;
Spinner cs1spin6;
Spinner cs1spin7;



protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);


    setContentView(R.layout.laycsesem1);
        ArrayAdapter<CharSequence> grade = ArrayAdapter.createFromResource(this,
    R.array.grade_list, android.R.layout.simple_spinner_item);
    grade.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    cs1spin1.setAdapter(sem_adapter);

            cs1spin1.setOnItemSelectedListener(this);

            @Override
      public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {

          // Completely blank after this.

        }

I need to apply the same for all the spinners. I have 7 in total in my XML file.


A method which I thought of was, Since my ITEMS on the Spinners can be converted to String, I could have used a switch (String) but Android is compiled under Java 1.5,1.6 ..and switch(String) is a featured in Java 1.7. So cannot use this.


Ashish Krishnan
  • 422
  • 2
  • 8
  • 16

3 Answers3

0

I suggest you to create a custom adapter that holds objects like

public class Grade{
    String mLabel;
    Float mValue;
.......
}

Look at that answer and this example.
So define that list of objects as a member of your Acticity/ Fragment.
After that in onItemSelected take you object by a given position.

Community
  • 1
  • 1
NickF
  • 5,637
  • 12
  • 44
  • 75
0

You can use a HashMap, which you can use 'A+' for example as a key, and 10 as a value. So, when the user clicks on 'A+', you can find in HashMap what is the key that has the value A+ and consequently you can find the value 10.

0

Here is an example how can you do it using Hashap:

    Map<String, Double> grades = new HashMap<String, Double>();

    grades.put("A+", 10.0);
    grades.put("B+", 8.5);

    //You can use grades.get(your_spinner.getSelectedItem())
    System.out.println(grades.get("A+"));