ArrayList資料add時出現覆蓋,及arraylist最後獲取到(儲存到集合中的)資料相同。
阿新 • • 發佈:2019-01-03
@Override public ArrayList<TaskDataPackage> getData() { System.out.println("getData()"); ArrayList<TaskDataPackage> dataListCount = new ArrayList<>(); TaskDataPackageList dataPackage; if (groupDataList != null) { // if (true)//判斷table 名-未完成 // { ArrayList<TaskDataPackageList> dataListNo = new ArrayList<>();for (InspectionTask inspectionTask : groupDataList) { dataPackage=new TaskDataPackageList(); dataPackage.setType(getTypeName(Integer.valueOf(inspectionTask.getInspectionMode()!=null?inspectionTask.getInspectionMode():"0"))); dataPackage.setTitle(inspectionTask.getTaskName()); dataPackage.setTime(inspectionTask.getTaskEndTime()); dataPackage.setProgress("3/6"); dataPackage.setMen("張三"); dataListNo.add(dataPackage); // dataPackage.setProgress(inspectionTask.getChildren().get().getExamObjectCount() + "/" + scc.getTotalObjectCount()); } TaskDataPackage taskDataPackageNo = new TaskDataPackage();taskDataPackageNo.setCount(groupDataList.size()); taskDataPackageNo.setTaskDataPackageList(dataListNo); dataListCount.add(taskDataPackageNo); // } else if(true){//已完成 ArrayList<TaskDataPackageList> dataListHas = new ArrayList<>();for (InspectionTask inspectionTask : groupDataList) { dataPackage=new TaskDataPackageList(); dataPackage.setType(getTypeName(Integer.valueOf(inspectionTask.getInspectionMode()!=null?inspectionTask.getInspectionMode():"0"))); dataPackage.setTitle(inspectionTask.getTaskName()); dataPackage.setTime(inspectionTask.getTaskEndTime()); dataPackage.setProgress("3/6"); dataPackage.setMen("李四"); dataListHas.add(dataPackage); // dataPackage.setProgress(inspectionTask.getChildren().get().getExamObjectCount() + "/" + scc.getTotalObjectCount()); } TaskDataPackage taskDataPackageHas = new TaskDataPackage();taskDataPackageHas.setCount(groupDataList.size()); taskDataPackageHas.setTaskDataPackageList(dataListHas); dataListCount.add(taskDataPackageHas); // }else {//已過期 ArrayList<TaskDataPackageList> dataListExp = new ArrayList<>();for (InspectionTask inspectionTask : groupDataList) { dataPackage=new TaskDataPackageList(); dataPackage.setType(getTypeName(Integer.valueOf(inspectionTask.getInspectionMode()!=null?inspectionTask.getInspectionMode():"0"))); dataPackage.setTitle(inspectionTask.getTaskName()); dataPackage.setTime(inspectionTask.getTaskEndTime()); dataPackage.setProgress("3/6"); dataPackage.setMen("趙六"); dataListExp.add(dataPackage); // dataPackage.setProgress(inspectionTask.getChildren().get().getExamObjectCount() + "/" + scc.getTotalObjectCount()); } TaskDataPackage taskDataPackageExp = new TaskDataPackage();taskDataPackageExp.setCount(groupDataList.size()); taskDataPackageExp.setTaskDataPackageList(dataListExp); dataListCount.add(taskDataPackageExp); // } } return dataListCount; }
解決辦法:ArraylistView :add方法中 資料,,都要重新new 一個。。確保 。add的內容所指向的地址,,部分之前add的資料 指向相同的地址。
原因: add 中的資料 所指向的地址 ,是同一個地址。即後面每次設定 的資料 都會覆蓋之前的資料。。所以 最後獲取的 arraylist的資料全都一樣。