2017-03-17 188 views
0

我在控制器包中的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; 
} 
+0

这听起来像是一种竞争条件。你确实知道学生模型是在stopCountdown()被调用之后添加的 – jay

+0

startCountdown()应该被调用的学生模型 –

回答

0

看起来像下面的检查需要做的:

  • studentPic是否不为空。
  • 无论mStudentModels不为空(主要是是否mStudentModels被stopCountdown()方法的调用之前获取添加)。

如果不能解决问题,请提供更多关于Controller端代码的信息。

+0

我已经编辑了上面的代码,请检查 –

+0

我只想更新numOfAbsences –

+0

事情是,如果我更新了stopCountDown()中的numOfAbsences,它也应该更新我的控制器的值。 –