我有一个小项目,可以创建一个计算学生GPA的应用程序。
到目前为止,我正在使用微调器让学生选择成绩。请注意,我只为一门课程创建了一个插槽,一旦运行正常,我将修改公式以计算所有课程。
一旦我单击了应该进行整理的按钮,就什么也没发生。
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.abdulelah.mygpa.GPA_51"
tools:showIn="@layout/activity_gpa_51">
<TextView
android:id="@+id/Result5_1"
android:layout_width="77dp"
android:layout_height="28dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.443" />
<Spinner
android:id="@+id/spinner5_1"
android:text="Spinner"
android:layout_width="93dp"
android:layout_height="28dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.029"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.036" />
<EditText
android:id="@+id/creditNum"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/Result5_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.232"
app:layout_constraintStart_toEndOf="@+id/spinner5_1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.021" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Result5_1" />
</android.support.constraint.ConstraintLayout>
Java代码
package com.example.abdulelah.mygpa;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class GPA_51 extends AppCompatActivity {
Spinner spinner;
double gradesValue;
double result;
EditText creditNum;
TextView result511;
Button ButtonResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpa_51);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
creditNum = (EditText)findViewById(R.id.creditNum);
ButtonResult = (Button)findViewById(R.id.button1);
result511 = (TextView)findViewById(R.id.Result5_1);
spinner = (Spinner) findViewById(R.id.spinner5_1);
final String[] grades = {"A+", "A", "B+", "B", "C+", "C", "D+", "D", "F"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, grades);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long id) {
parent.getItemAtPosition(i);
if (grades.equals("A+"))
gradesValue=4.00;
else if (grades.equals("A"))
gradesValue=3.75;
else if (grades.equals("B+"))
gradesValue=3.50;
else if (grades.equals("B"))
gradesValue=3.00;
else if (grades.equals("C+"))
gradesValue=2.50;
else if (grades.equals("C"))
gradesValue=2.00;
else if (grades.equals("D+"))
gradesValue=1.50;
else if (grades.equals("D"))
gradesValue=1.00;
else if (grades.equals("F"))
gradesValue=0;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
ButtonResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String creditNumber1 = creditNum.getText().toString();
int numberOfCredit = Integer.parseInt(creditNumber1);
result = gradesValue * numberOfCredit;
result511.setText(String.valueOf(result));
}
});
}
}
答案 0 :(得分:1)
您应该通过这种方式检查
if(parent.getItemAtPosition(position).toString().equalsIgnoreCase("A+")){gradesValue=4.00;}
答案 1 :(得分:1)
因为您正在比较错误的变量。 grades
是一个字符串数组。
String selectedGrade = parent.getItemAtPosition(i);
//now use
if(selectedGrade.equals("A+")){
}...