从两个可充气的Edittext数组列表中获取值并计算值

时间:2018-03-23 10:34:11

标签: java android arraylist android-edittext layout-inflater

我目前正在制作一个程序,我可以使用两个充气的edittexts并用它们进行计算。所以我试图获取每个值,将它们放入指定的arraylist并根据相应的数组计算值,但我遇到麻烦,因为我得到一个indexoutofbounds异常,我不知道我的代码是否正常工作。我能得到一些帮助吗?

这是我的代码。

public class CalculateActivity extends AppCompatActivity {
Button calculate;
LinearLayout courses;
ArrayList<Course> coursesList;
SemesterDatabase db;
TextView gpa;
int percentage = 0;
int hundredth =0;
float averageGrade = 0.0f;

float[] gradePoints = new float[]{};


ArrayList<Float> gradesList = new ArrayList();
ArrayList<Float> scoreList = new ArrayList();
ArrayList<Float> totalList = new ArrayList();



int id;
TextView semester;

class C03171 implements OnClickListener {
    C03171() {
    }

     public void onClick(View view) {
        float gradePointsCalc = 0.0f;
        float totalCrdtHrs = 0.0f;

//编辑Jyoti JK,谢谢你的帮助

            for (int x = 0; x < scoreList.size()&& x<totalList.size(); x++)

        {CalculateActivity.this.gradesList.add(scoreList.get(x) / totalList.get(x));
        }



        for (int i = 0; i < CalculateActivity.this.coursesList.size(); i++) {
            int crdtHrs = ((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours();
            totalCrdtHrs += (float) crdtHrs;
            averageGrade += (((Float)CalculateActivity.this.gradesList.get(i).floatValue())*(((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours())/totalCrdtHrs);

        }
        float gpaCalc = averageGrade;
        CalculateActivity.this.gpa.setText(String.format("%.3f", new Object[]{Float.valueOf(gpaCalc)}));
    }
}


class C03194 implements DialogInterface.OnClickListener {
    C03194() {
    }

    public void onClick(DialogInterface dialogInterface, int i) {
        dialogInterface.cancel();
    }
}

class C03205 implements DialogInterface.OnClickListener {
    C03205() {
    }

    public void onClick(DialogInterface dialogInterface, int i) {
        CalculateActivity.this.db.deleteSemester(CalculateActivity.this.id);
        CalculateActivity.this.db.deleteAllCourses(CalculateActivity.this.id);
        CalculateActivity.this.onBackPressed();
    }
}



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView((int) R.layout.activity_calculate);
    setSupportActionBar((Toolbar) findViewById( R.id.toolbar));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle((CharSequence) "Calculate GPA");
    this.db = new SemesterDatabase(this);
    this.id = getIntent().getIntExtra("semesterId", 1);
    this.coursesList = this.db.getAllCourses(this.id);
    this.semester = (TextView) findViewById(R.id.semester);
    this.semester.setText(getIntent().getStringExtra("semesterName"));
    this.gpa = (TextView) findViewById(R.id.gpa);
    this.calculate = (Button) findViewById(R.id.calculate);
    this.courses = (LinearLayout) findViewById(R.id.courses);
    addCourses();
    this.calculate.setOnClickListener(new C03171());

}

public void addCourses() {
    LayoutInflater inflater = LayoutInflater.from(this);
    for (int i = 0; i < this.coursesList.size(); i++) {
        View v = inflater.inflate(R.layout.item_course, null, false);
        this.courses.addView(v);
        final int position = CalculateActivity.this.scoreList.size();
        final int position2 = CalculateActivity.this.totalList.size();
        CalculateActivity.this.scoreList.add(0.0f);
        final EditText Score = (EditText) v.findViewById(R.id.Score);
        final EditText Total = (EditText) v.findViewById(R.id.Total);

        Score.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }


            public void afterTextChanged(Editable editable) {
                percentage = Integer.parseInt(editable.toString());
                float perc = (float) percentage;
                CalculateActivity.this.scoreList.set(position, perc);

            }
        });

        Total.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void afterTextChanged(Editable editable) {
                hundredth = Integer.parseInt(editable.toString());
                float hund = (float) percentage;
                CalculateActivity.this.scoreList.set(position, hund);

            }
        });




    }
}

基本上我认为我的代码正在做的是从

获取edittext值
 Score.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }


            public void afterTextChanged(Editable editable) {
                percentage = Integer.parseInt(editable.toString());
                float perc = (float) percentage;
                CalculateActivity.this.scoreList.set(position, perc);

            }
        });

        Total.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            public void afterTextChanged(Editable editable) {
                hundredth = Integer.parseInt(editable.toString());
                float hund = (float) percentage;
                CalculateActivity.this.scoreList.set(position, hund);

            }
        });

获取值

for (int x = 0; x < scoreList.size(); x++)

        {
            ArrayList<Float> finalscore = new ArrayList();
            finalscore.add(scoreList.get(x) / totalList.get(x));
            for (int j = 0; j < finalscore.size(); j++)

            {
                CalculateActivity.this.gradesList.add(finalscore.get(j));
            }
        }

最后使用

计算它
 for (int i = 0; i < CalculateActivity.this.coursesList.size(); i++) {

             averageGrade += ((Float)CalculateActivity.this.gradesList.get(i).floatValue())*(((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours());

        }
        float gpaCalc = averageGrade;
        CalculateActivity.this.gpa.setText(String.format("%.3f", new Object[]{Float.valueOf(gpaCalc)}));

但我绝对不知道哪里出错了,我不知道怎么看错了。我希望得到详细的评论!

哦,这是错误日志。

FATAL EXCEPTION: main
                                                                             Process: com.example.joon.chadwickgrades, PID: 19395
                                                                             java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                                                 at java.util.ArrayList.get(ArrayList.java:411)
                                                                                 at com.example.joon.chadwickgrades.CalculateActivity$C03171.onClick(CalculateActivity.java:66)
                                                                                 at android.view.View.performClick(View.java:5623)
                                                                                 at android.view.View$PerformClick.run(View.java:22433)
                                                                                 at android.os.Handler.handleCallback(Handler.java:751)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6247)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

1 个答案:

答案 0 :(得分:0)

作为您的代码,

for (int x = 0; x < scoreList.size(); x++)

    {
        ArrayList<Float> finalscore = new ArrayList();
        finalscore.add(scoreList.get(x) / totalList.get(x));
        for (int j = 0; j < finalscore.size(); j++)

        {
            CalculateActivity.this.gradesList.add(finalscore.get(j));
        }
    }

对于每次迭代,

1.您正在创建一个finalscore arraylist对象。

2.只添加一个元素。

3.然后内部for循环开始。 finalscore的大小总是一个。并且您将它添加到等级列表。

我认为你可以改变它,

 for (int x = 0; x < scoreList.size() && x<totalList.size(); x++)

    {

       CalculateActivity.this.gradesList.add(scoreList.get(x) / totalList.get(x));

    }

并更改代码,

for (int i = 0; i < CalculateActivity.this.coursesList.size() && i <CalculateActivity.this.gradesList.size(); i++) {
            int crdtHrs = ((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours();
            totalCrdtHrs += (float) crdtHrs;
            averageGrade += (((Float)CalculateActivity.this.gradesList.get(i).floatValue())*(((Course) CalculateActivity.this.coursesList.get(i)).getCreditHours())/totalCrdtHrs);

        }

修改

关键是,每当您从列表中获取数据时,您需要检查它是否为空以及它的大小。这是避免异常的最佳做法