代码之家  ›  专栏  ›  技术社区  ›  Junseok Joon Kang

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

  •  1
  • Junseok Joon Kang  · 技术社区  · 6 年前

    我目前正在开发一个程序,在这个程序中,我可以获取两个充气编辑文本并用它们进行计算。因此,我试图获取每个值,将它们放入指定的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);
    
                }
            });
    
    
    
    
        }
    }
    

    基本上,我认为我的代码所做的是从

     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 回复  |  直到 6 年前
        1
  •  0
  •   Jyoti JK    6 年前

    作为您的代码,

    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、只添加一个元素。

    然后内部for循环开始。finalscore的大小始终为1。您正在将其添加到成绩列表中。

    我想你可以改成,

     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);
    
            }
    

    编辑:

    关键是,无论何时从列表中获取数据,都需要 检查它是否为空,以及它的大小。 这是避免例外的最佳实践