Spidey
Spidey

Reputation: 397

How to use loop to list instances content?

I am new to android development. In the following, I know how to loop through the school list. However, I don't know how to use loop to display the the students in school, because each school instances is manually created. Is there a way I can display each school and its students using loop? Thanks.

My data is as followed:

school id: A1, "Point Grey"
Student name: "Kent", "Age": 43
Student name: "Jane", "Age": 18
Student name: "Winnie", "Age": 19 

school id: B1, "Prince of Wales"
Student name: "Steven", "Age": 11
Student name: "Michelle", "Age": 30
Student name: "Ronald", "Age": 20

My Directory is as followed:

public class DIRECTORY {
   private Map<String, SCHOOL> schools = new HashMap<String, SCHOOL>();


    public void setSchools(Map<String, SCHOOL> schoolsMap) {
    this.schools =  schoolsMap;
    }

 public Map<String, SCHOOL> getSchools() {return this.schools;}


 public void addSchool(String schoolId, String schoolName) {
     SCHOOL school = new SCHOOL();
     school.setSchoolId(schoolId);
     school.setSchoolName(schoolName);
     schools.put(schoolId,school);

   }

    public SCHOOL getSchoolById(String schoolId) {
        schools.get(schoolId);
    }

   }

My School class as followed:

public class SCHOOL {
private String schoolId;
private String schoolName;
private List<STUDENT> students = new ArrayList<>();

//getter and setter for schoolid and school name 
public void setStudents(STUDENT students) {
    this.students = (List<STUDENT>) students;
}
public List<STUDENT> getStudents() {return this.students;}
public void addStudent(String names, String age) {
    STUDENT student = new STUDENT();
    student.setNames(names);
    student.setAge(age);
    students.add(student);
}

My student class as followed:

public class STUDENT {
private String names;
private String age;
//getter and setter for name and age

}

My data is as followed:

private static DIRECTORY createDummySchool(){

    DIRECTORY directory = new DIRECTORY();
    directory.addSchool("A1", "Point Grey");
    directory.addSchool("B1", "Prince of Wales");

    SCHOOL schoolA1 = directory.getSchoolById("A1");

    schoolA1.addStudent("kent", "43");
    schoolA1.addStudent("Winnie", "19");
    schoolA1.addStudent("Jane", "18");

    SCHOOL schoolB1 = directory.getSchoolById("B1");
    schoolB1.addStudent("Steven", "11");
    schoolB1.addStudent("Michelle", "30");
    schoolB1.addStudent("Ronald", "20");

    return directory;
}

My Directory List as followed:

public String outputDirectorylist(List<DIRECTORY> directorylist) {
    StringBuilder sb = new StringBuilder();
    for (DIRECTORY directory:directorylist) {
        sb.append(school.getSchoolId());
        sb.append(",");
        sb.append(school.getSchoolName());
        sb.append(";");
    }

    return String.valueOf(sb);

}

My Main class is as followed:

 .....
 DIRECTORY directorylist = directory.getSchools();
 // How to loop the student in each school?
 Log.d(tag, outputSchool(directorylist));
 .....

Upvotes: 0

Views: 52

Answers (2)

laune
laune

Reputation: 31290

Map<String,SCHOOL> schools = directory.getSchools();
for( SCHOOL school: schools.values() ){
    for( STUDENT student: school.getStudents() ){
        Log.d( ... student.getName() + " " + student.getAge() ... );
    }
}

Assuming there is a getStudents in the SCHOOL class, and that getSchools returns the Map from class DIRECTORY.

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157447

one way would be to override toString() in your classes.

E.g.

public class STUDENT {
     private String names;
     private String age;
    //getter and setter for name and age

    public String toString() {
        StringBuilder b = new StringBuilder();
        b.append("name: ");
        b.append(names == null ? " " : names);
        b.append(" age: ");
        b.append(age == null ? " " : age);
        return b.toString();
    }
}

calling Log.d("LOG", " " + listOfStundes) will print the content of listOfStundes, as defined in toString(). You can apply the same principle to the other class of yours

Upvotes: 1

Related Questions