更新arraylist

时间:2017-03-17 21:56:11

标签: java android arraylist

我在控制器包中的arraylist中有一个静态数据。这些是我在控制器包中的代码。

       for(int i=0;i<studentPic.length;i++){
          //i want to update the value of zero
         mStudentModels.add(new StudentModel(studentPic[i],status[i],studentName[i],courseAndYear[i],0));
       }

       public List<StudentModel> getStudentModels (){
       return mStudentModels;
       }

现在我想更新某个数据,如果它符合某个条件:

     private void stopCountdown() {
    if (mTask != null) {
        mTask.interrupt();
        List<StudentModel> studentModels = studentData.getStudentModels();
        studentData = new StudentData();
        for(int i=0;i<studentModels.size();i++){
            StudentModel studentModel = studentModels.get(i);
            if(studentModels.get(i).getStatus()=="PRESENT"){
                mFirebaseDatabase.child("attendance").child(formattedDate).child("present").push().setValue(studentModel);
            }else if(studentModels.get(i).getStatus()=="LATE"){
                mFirebaseDatabase.child("attendance").child(formattedDate).child("late").push().setValue(studentModel);
            }else{
                 //i want to update the value of  zero here
                int absences = studentModel.getNumOfAbsences();
                studentModel.setNumOfAbsences(absences+1);
                mFirebaseDatabase.child("attendance").child(formattedDate).child("absent").push().setValue(studentModel);
            }
        }


    }
}

问题是如果我调用studentData.getStudentModels();值始终为零。问题是什么?

这是我的控制者:         公共类StudentData {

private List<StudentModel> mStudentModels;

public void setmStudentModels(List<StudentModel> mStudentModels) {
    this.mStudentModels = mStudentModels;
}

public StudentData(){
    mStudentModels = new ArrayList<>();
    int[] studentPic = {R.drawable.boy1, R.drawable.boy2, R.drawable.girl1, R.drawable.girl2, R.drawable.girl3,
            R.drawable.girl4, R.drawable.girl5, R.drawable.boy3, R.drawable.girl6, R.drawable.girl7,
            R.drawable.boy4, R.drawable.girl8, R.drawable.girl9, R.drawable.girl10, R.drawable.boy5,
            R.drawable.girl1, R.drawable.girl12, R.drawable.girl13, R.drawable.boy6, R.drawable.girl14};

    String[] status = {"ABSENT","LATE","PRESENT","PRESENT","PRESENT","PRESENT","PRESENT","PRESENT","PRESENT","PRESENT","PRESENT","PRESENT",
            "PRESENT","PRESENT","PRESENT","ABSENT","PRESENT","PRESENT","PRESENT","PRESENT"};
    String[] studentName = {"Andy Lim", "Benjamin Adams", "Karen Healey", "Anne Pierre", "Corine Alvarez",
            "Emily Hedrick", "Courtney Quick", "Oscar Renteria", "Ellen Joy", "Jesse Ramer",
            "Jackson Beck", "Alicia Hofer", "Jenae Rupp", "Allison Beitler", "Martin Hofkamp",
            "Emma Ruth", "Kathryn Berg", "Michelle Salgado", "Lewis Caskey", "Elizabeth Core"};
    String[] courseAndYear = {"BSIT-3", "BSIT 2", "BSIT-3","BSCS-3" , "BSCS-2", "BSIT-3", "BSIT-2", "BSIT-3", "BSIT-2", "BSIT-3",
            "BSCS-3", "BSCS-2", "BSCS-3", "BSCS-2", "BSCS-3", "BSIT-3", "BSCS-2", "BSIT-2", "BSCS-3", "BSCS-2"};


    for(int i=0;i<studentPic.length;i++){
         mStudentModels.add(new StudentModel(studentPic[i],status[i],studentName[i],courseAndYear[i],0));
    }




}

public List<StudentModel> getStudentModels (){
    return mStudentModels;
}

}

我的模型看起来像这样:

public StudentModel(int studentPic, String status, String studentName, String courseAndYear, int numOfAbsences) {
    this.studentPic = studentPic;
    this.status = status;
    this.studentName = studentName;
    this.courseAndYear = courseAndYear;
    this.numOfAbsences = numOfAbsences;
}

1 个答案:

答案 0 :(得分:0)

看起来需要执行以下检查:

  • studentPic是否为空。
  • mStudentModels是否为空(主要是在调用stopCountdown()方法之前是否添加了mStudentModels)。

如果无法解决问题,请提供有关控制器端代码的更多信息。