Android微调器:如何为它们编写java代码?

时间:2013-07-17 00:22:51

标签: java android android-arrayadapter android-spinner onclicklistener

我正在尝试制作GPA计算器。我在两列中有一堆微调器。每个7个。我已经在下拉列表中放入了一个字符串数组。现在,我想在java中编写代码,用于说明用户是否从下拉列表中选择“4”,然后输入应该被视为数字4以便将其乘以例如用户选择等级A,然后乘以4.如何做到这一点?我创建了一个数组适配器,但它不起作用。我得到“楠”作为答案。我查了一下,并且当数字未定义时显示Nan显示。任何帮助,将不胜感激。谢谢!

我应该使用某种类型的onClickListener,就像我用于我的按钮一样吗?如果用户选择A级,计算机如何知道必须将gradepoint等于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;

2 个答案:

答案 0 :(得分: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
}

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

答案 1 :(得分: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);

}

希望它有所帮助。