2017-03-17 189 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,它也應該更新我的控制器的值。 –