0

我正在尝试制作 GPA 计算器。我在两列中有一堆微调器。每个 7 个。我已经放入了要在下拉列表中显示的字符串数组。现在,我想用java编写代码来说明如果用户从下拉列表中选择“四”,那么输入应该被认为是数字4,然后再乘以例如如果用户选择A级,然后乘以与 4. 我该怎么做?我创建了一个阵列适配器,但它不工作。我得到“Nan”作为答案。我查了一下,它说当数字未定义时会显示 Nan。任何帮助,将不胜感激。谢谢!

我应该使用某种 onClickListener,就像我用于按钮一样吗?如果用户选择 A 级,计算机如何知道它必须将等级点等同于 4?

这就是我所做的(这显然是错误的):

if(spinner1.getSelectedItem().equals("A+")) gradepoint1=4.00;
        if(spinner1.getSelectedItem().equals("A")) gradepoint1=4.00;
        if(spinner1.getSelectedItem().equals("A-")) gradepoint1=3.67;
        if(spinner1.getSelectedItem().equals("B+")) gradepoint1=3.33;
        if(spinner1.getSelectedItem().equals("B")) gradepoint1=3.00;
        if(spinner1.getSelectedItem().equals("B-")) gradepoint1=2.67;
        if(spinner1.getSelectedItem().equals("C+")) gradepoint1=2.33;
        if(spinner1.getSelectedItem().equals("C")) gradepoint1=2.00;
        if(spinner1.getSelectedItem().equals("C-")) gradepoint1=1.67;
        if(spinner1.getSelectedItem().equals("D+")) gradepoint1=1.33;
        if(spinner1.getSelectedItem().equals("D")) gradepoint1=1.00;
        if(spinner1.getSelectedItem().equals("D-")) gradepoint1=0.67;
        if(spinner1.getSelectedItem().equals("F")) gradepoint1=0.00;
        if(spinner1.getSelectedItem().equals("ABS")) gradepoint1=0.00;
4

2 回答 2

0

您可以拥有值的哈希图,然后根据所选项目选择值

HashMap<String, Double> mGrades;

public void test(){
    Spinner spinner1 = new Spinner(this);

    mGrades = new HashMap<String, Double>();
    mGrades.put("A+", 4.00);
    mGrades.put("A", 4.00);
    //Etc.....

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mGrades.keySet().toArray(new String[0]));

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner1.setAdapter(adapter);

    spinner1.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
    Log.d("TAG", String.valueOf(mGrades.get(mGrades.keySet().toArray()[pos])));
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}

使用其他方法可能会慢一些,但是嘿。它很简单。

于 2013-07-17T00:44:20.633 回答
0

我建议你这样做:

在你开始之前:

float[] gradeValues = { 4f, 3.67f, 3.33f, 3.00f, 2.67f, 2.33f, 2.00f,1.33f,1.00f,0.67f,0.00f,0.00f};

String [] gradeAlpabetic = {"A+","A","A-","B+","B","B-","C+","C","C-","D+","D","D-","F","ABS"};

这里第一个值 4f 的位置为 0,下一个值为 1,依此类推。

值“A+”将位于微调器上的位置 0,因此我们知道哪个等级与哪个等级值对应。

在 onViewCreated 或 OnCreate 中:

ArrayAdapter<String> gradesArrayAdap = new ArrayAdapter<String>(
            getActivity(), R.layout.spinnerlayout, gradeAlphabetic);

    gradesSpinner.setAdapter(GradesArrayAdapter);


  gradeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            getGrades();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    });

然后你把它放在 onViewCreated/onCreate 之外。

public void getGrades(){


       String grade = gradeSpinner.getSelectedItemPosition();
       float theGrade = Float.parseFloat(gradeValue(grade);

      System.out.println("Your grade in float is: " + theGrade);

}

希望能帮助到你。

于 2013-07-17T01:36:05.630 回答