最后一个数据从arraylist检索上面的数据无法检索

时间:2017-09-17 09:07:54

标签: android arrays

存储在ArrayList中的JSON数据。所有数据都存储得很完美,但是当我尝试从ArrayList获取它们时,ArrayList只获取最后一条记录。

MainActivity:

ArrayList<ATTENDANCE> empJsonList = new ArrayList<>();
ATTENDANCE attend = new ATTENDANCE();

if (jsonArray != null) {
    for (int j = 0; j < 1; j++) {
        JSONObject jsonObject = jsonArray.getJSONObject(j);

        UserName = jsonObject.getString("StudentName");
        RollNo = jsonObject.getString("RollNo");
        Standard = jsonObject.getString("Standard");
        Division = jsonObject.getString("Division");

        attend.setUserName(UserName);
        attend.setRollNo(RollNo);
        attend.setStandard(Standard);
        attend.setDivision(Division);

        // Log.i("result", s);
    }

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
        String date = jsonObject1.getString("AttendanceDate");
        //Dirty conversion
        String replaceDate = date.replace("/Date(", "").replace(")/", "");
        Long getDate = Long.valueOf(replaceDate);
        //calendar.setTimeInMillis(timeInMillis);
        //AttendanceDate = calendar.getTime().toGMTString();
        AttendanceDate = dateFormat.format(getDate);
        Attendance_Type = jsonObject1.getString("Attendance_Type");
        Remark = jsonObject1.getString("Remark");

        attend.setAttendanceDate(AttendanceDate);
        attend.setAttendance_type(Attendance_Type);
        attend.setRemark(Remark);

        try {
            empJsonList.add(attend);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

ATTENDANCE:

private String standard, division, stdDiv, attendance_type, remark, attendanceDate, userName;
private String rollNo;
private  int id;

public String getStandard() {
    return standard;
}

public void setStandard(String standard) {
    this.standard = standard;
}

public String getDivision() {
    return division;
}

public void setDivision(String division) {
    this.division = division;
}

public String getAttendance_type() {
    return attendance_type;
}

public void setAttendance_type(String attendance_type) {
    this.attendance_type = attendance_type;
}

public String getRemark() {
    return remark;
}

public void setRemark(String remark) {
    this.remark = remark;
}

public String getAttendanceDate() {
    return attendanceDate;
}

public void setAttendanceDate(String attendanceDate) {
    this.attendanceDate = attendanceDate;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getRollNo() {
    return rollNo;
}

public void setRollNo(String rollNo) {
    this.rollNo = rollNo;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public  String toString()
{
    return  id + " : " + userName + " : " + standard + " : " + division + " : " + rollNo
        + " : " + attendance_type + " : " + attendanceDate + " : " + remark ;

    //return "id : " + id + "userName : " + userName + "standard:" + standard 
    //    + "division: " + division + "rollNo:" + rollNo +
}

1 个答案:

答案 0 :(得分:0)

您再次使用同一个对象添加新数据:

ATTENDANCE attend = new ATTENDANCE();

在设置数据之前,您应该将上面的行放在for循环中,以确保添加新对象:

ATTENDANCE attend = new ATTENDANCE();
attend.setAttendanceDate(AttendanceDate);
attend.setAttendance_type(Attendance_Type);
attend.setRemark(Remark);
empJsonList.add(attend);