Skip to content

Commit 672c803

Browse files
authored
Merge pull request #34 from stgm/labified
week 3
2 parents 6fd18f9 + 94fd4f6 commit 672c803

13 files changed

+138
-199
lines changed

Week3/Content.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,18 @@ In Android, you often want to display your lists of data in an appropriate way.
6262

6363
This is where the `Adapter` class comes into play. Because the Android API `Adapter` class can be overwhelming at first glance, we have created a plain Java adapter that kind of does the same thing, but takes it down to the basics. Its prime functionality is that it can take in a list of data, either a regular array or an `ArrayList` and usually some kind of layout definition.
6464

65-
It then combines these two to generate the appropriate layout for each element in the list. This layout is then shown in a container of some sort. In this example we just use the terminal, but we have still constructed a container class to represent the idea of a container.
65+
It then combines these two to generate the appropriate layout for each element in the list. This layout is then shown in a container of some sort. In this example we just use the terminal, but we have still constructed a printer class to represent the idea of a printer.
6666

6767
The adapter class can be instantiated using the constructor, which creates the `ArrayAdapter` object. In this example, we created an adapter that can create a (very simple) graphical representation of `Student` objects, but many kinds of adapters are of course possible!
6868

6969
class ArrayAdapter {
7070
7171
private Student[] studentArray;
72-
private LayoutType layoutType;
72+
private Formatter formatter;
7373
74-
public ArrayAdapter(Student[] studentArray, LayoutType layoutType) {
74+
public ArrayAdapter(Student[] studentArray, Formatter formatter) {
7575
this.studentArray = studentArray;
76-
this.layoutType = layoutType;
76+
this.formatter = formatter;
7777
}
7878
7979
public String createRow(int index) {
@@ -83,8 +83,8 @@ The adapter class can be instantiated using the constructor, which creates the `
8383
8484
Student currentStudent = studentArray[index];
8585

86-
horizontalBorder = layoutType.horizontalBorder;
87-
verticalBorder = layoutType.verticalBorder;
86+
horizontalBorder = formatter.horizontalBorder;
87+
verticalBorder = formatter.verticalBorder;
8888

8989
row = horizontalBorder + verticalBorder + " " + currentStudent.getName() + " " + horizontalBorder;
9090
return row;
@@ -97,27 +97,27 @@ The adapter class can be instantiated using the constructor, which creates the `
9797

9898
The following enum contains the information about the layout. It does not much, except determine that certain string patterns belong with a certain name. This avoids having to define the options as constants elsewhere in the code and keeps everything layout related neatly together.
9999

100-
enum LayoutType {
100+
enum Formatter {
101101
DASH("\n---------------------\n", "|"),
102102
CIRCLE("\no 0 o 0 o 0 o 0 o 0 o\n", "0"),
103103
STAR("\n*********************\n", "*");
104104
105105
String horizontalBorder, verticalBorder;
106106
107107
// Constructor
108-
private LayoutType(String horizontalBorder, String verticalBorder) {
108+
private Formatter(String horizontalBorder, String verticalBorder) {
109109
this.horizontalBorder = horizontalBorder;
110110
this.verticalBorder = verticalBorder;
111111
}
112112
}
113113

114-
As mentioned before, the adapter is not used on its own, it is paired with a list container. This list container is in control how much room there is on the screen, or how many rows we actually want to show. On a screen with limited space, like a phone, you don't want to do unnecessary work, thus you only want to ask the adapter to do things for the items you have room for. We simulate this in the terminal by having a predetermined number of rows that we want to show.
114+
As mentioned before, the adapter is not used on its own, it is paired with a list printer. This list printer is in control how much room there is on the screen, or how many rows we actually want to show. On a screen with limited space, like a phone, you don't want to do unnecessary work, thus you only want to ask the adapter to do things for the items you have room for. We simulate this in the terminal by having a predetermined number of rows that we want to show.
115115

116-
class ListContainer {
116+
class Printer {
117117
118118
private int rowsToShow;
119119

120-
public ListContainer(int rowsToShow) {
120+
public Printer(int rowsToShow) {
121121
this.rowsToShow = rowsToShow;
122122
}
123123
@@ -129,11 +129,11 @@ As mentioned before, the adapter is not used on its own, it is paired with a lis
129129
}
130130
}
131131

132-
Because the container class is in control of the amount of rows shown, it is also the one that is used to iterate over the items in the list inside the `setAdapter()` method.
132+
Because the printer class is in control of the amount of rows shown, it is also the one that is used to iterate over the items in the list inside the `setAdapter()` method.
133133

134-
Through the instance of `ArrayAdapter`, it calls the `createRow()` method which then creates the "layout" using the strings defined in the `enum`. The method then uses the index it received from the container class to get the right `Student` object out of the array. With this object, the correct data can be displayed in the row. This string representing the row is returned to the instance of `ListContainer` that called it, which then prints the result to the terminal.
134+
Through the instance of `ArrayAdapter`, it calls the `createRow()` method which then creates the "layout" using the strings defined in the `enum`. The method then uses the index it received from the printer class to get the right `Student` object out of the array. With this object, the correct data can be displayed in the row. This string representing the row is returned to the instance of `Printer` that called it, which then prints the result to the terminal.
135135

136-
What is essential is that there is some kind of information on what the layout should look like (what kind of border to use), and there is a list (the student objects). To determine how many elements are shown, there is also a container. This information is then combined, having the container determine what index should be rendered, which is then passed on to the adapter, which generates the appropriate layout for every item.
136+
What is essential is that there is some kind of information on what the layout should look like (what kind of border to use), and there is a list (the student objects). To determine how many elements are shown, there is also a printer. This information is then combined, having the printer determine what index should be rendered, which is then passed on to the adapter, which generates the appropriate layout for every item.
137137

138138
This is all very similar to what the actual adapter class does in Android Studio. But more on that later!
139139

@@ -153,7 +153,7 @@ This time we will not write that much new code ourselves, but mostly use existin
153153

154154
You are encouraged to discuss the purpose of the code with others (without completely spoiling the answer of course) and asking the TA's if in doubt! Adapters are a recurring topic in the course so a good understanding of them will go a long way!
155155

156-
2. Although the adapter class is given to you, it is not called yet in our plain Java example. Think about what the adapter needs to be instantiated and pass the correct parameters to it. Since the adapter goes together with the `ListContainer`, you need to instantiate this one as well. After instantiating both, call `setAdapter()`.
156+
2. Although the adapter class is given to you, it is not called yet in our plain Java example. Think about what the adapter needs to be instantiated and pass the correct parameters to it. Since the adapter goes together with the `Printer`, you need to instantiate this one as well. After instantiating both, call `setAdapter()`.
157157

158158
3. Reset the adapter, but with a different `enum` as a parameter. What happens when you do this?
159159

@@ -179,7 +179,7 @@ Because we use the Android API and not just plain Java, a lot of functionality i
179179

180180
This will leave us with some things to take care of: we need to be able to instantiate our adapter and we need to define what should be rendered for every entry in our list. This is similar to the functionalities of the plain Java adapter class, which also had some representation of a layout (the `enum`) and of course a constructor.
181181

182-
Like with our plain Java class, the actual iteration is handled behind the scenes. In the plain Java example, the `ListContainer` class handled making calls to the adapter's `createRow()` method. In Android, the iteration is handled by the list container as well. This means that there is no need to iterate over your list inside the adapter class.
182+
Like with our plain Java class, the actual iteration is handled behind the scenes. In the plain Java example, the `Printer` class handled making calls to the adapter's `createRow()` method. In Android, the iteration is handled by the list printer as well. This means that there is no need to iterate over your list inside the adapter class.
183183

184184
<a name="adding-constructor"></a>
185185

@@ -291,12 +291,12 @@ Notice that we return the altered `convertView`? Using this method, we now made
291291
<a name="setup"></a>
292292

293293
### Setting everything up
294-
Now that the adapter has been defined and the methods inside as well, we can couple it with a list container. This, again, is similar to the plain Java example. The container is initialized at some point, then the adapter is created using its constructor. Finally, the adapter is passed to the container.
294+
Now that the adapter has been defined and the methods inside as well, we can couple it with a list printer. This, again, is similar to the plain Java example. The printer is initialized at some point, then the adapter is created using its constructor. Finally, the adapter is passed to the printer.
295295

296296
StudentAdapter adapter = new StudentAdapter(this, R.layout.row_item, studentList);
297297
listView.setAdapter(adapter);
298298

299-
If you do not set your adapter on the container, nothing will happen on the screen yet. The actual rendering of list items only starts once the adapter is connected to the container using `setAdapter()`.
299+
If you do not set your adapter on the printer, nothing will happen on the screen yet. The actual rendering of list items only starts once the adapter is connected to the printer using `setAdapter()`.
300300

301301

302302
<a name="practice"></a>
@@ -328,5 +328,5 @@ This time we will also practice with code in Android Studio, to try and make the
328328
## Wrapping it up
329329
The adapter sees a lot of use in various apps, not just in this course. A lot of apps use lists of items, whether horizontally, vertically or on a grid. The items shown in the adapter can contain all kinds of view: text, images, buttons, checkboxes, etc. Different variations of adapters exist, but the basic principles are the same.
330330

331-
Adapters can be used to render lists of custom objects, like the `Student` object, which requires you to create your custom adapter class and define the contents of `getView()`. In this tutorial, we saw what an adapter and list container do behind the scenes, using a plain Java example. The tasks boil down to iterating over the list, which is handled by the list container, and rendering the correct contents, which is handled by the adapter. The adapter and the container are strongly connected, because the container dictates what items need to be shown and thus what items should be created by the adapter.
331+
Adapters can be used to render lists of custom objects, like the `Student` object, which requires you to create your custom adapter class and define the contents of `getView()`. In this tutorial, we saw what an adapter and list printer do behind the scenes, using a plain Java example. The tasks boil down to iterating over the list, which is handled by the list printer, and rendering the correct contents, which is handled by the adapter. The adapter and the printer are strongly connected, because the printer dictates what items need to be shown and thus what items should be created by the adapter.
332332

Week3/Java/AdapterTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public int getStudentNumber() {
5353
/*
5454
...
5555
*/
56-
class ListContainer {
56+
class Printer {
5757

5858
private int rowsToShow;
5959

60-
public ListContainer(int rowsToShow) {
60+
public Printer(int rowsToShow) {
6161
this.rowsToShow = rowsToShow;
6262
}
6363

@@ -72,15 +72,15 @@ public void setAdapter(ArrayAdapter adapter) {
7272
/*
7373
...
7474
*/
75-
enum LayoutType {
75+
enum Formatter {
7676
DASH("\n---------------------\n", "|"),
7777
CIRCLE("\no 0 o 0 o 0 o 0 o 0 o\n", "0"),
7878
STAR("\n*********************\n", "*");
7979

8080
String horizontalBorder, verticalBorder;
8181

8282
// Constructor
83-
private LayoutType(String horizontalBorder, String verticalBorder) {
83+
private Formatter(String horizontalBorder, String verticalBorder) {
8484
this.horizontalBorder = horizontalBorder;
8585
this.verticalBorder = verticalBorder;
8686
}
@@ -92,11 +92,11 @@ private LayoutType(String horizontalBorder, String verticalBorder) {
9292
class ArrayAdapter {
9393

9494
private Student[] studentArray;
95-
private LayoutType layoutType;
95+
private Formatter formatter;
9696

97-
public ArrayAdapter(Student[] studentArray, LayoutType layoutType) {
97+
public ArrayAdapter(Student[] studentArray, Formatter formatter) {
9898
this.studentArray = studentArray;
99-
this.layoutType = layoutType;
99+
this.formatter = formatter;
100100
}
101101

102102
public String createRow(int index) {
@@ -106,8 +106,8 @@ public String createRow(int index) {
106106

107107
Student currentStudent = studentArray[index];
108108

109-
horizontalBorder = layoutType.horizontalBorder;
110-
verticalBorder = layoutType.verticalBorder;
109+
horizontalBorder = formatter.horizontalBorder;
110+
verticalBorder = formatter.verticalBorder;
111111

112112
row = horizontalBorder + verticalBorder + " " + currentStudent.getName() + " " + horizontalBorder;
113113
return row;

Week3/Java/AdapterTestFull.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public int getStudentNumber() {
5050
}
5151
}
5252

53-
class ListContainer {
53+
class Printer {
5454

5555
private int rowsToShow;
5656

57-
public ListContainer(int rowsToShow) {
57+
public Printer(int rowsToShow) {
5858
this.rowsToShow = rowsToShow;
5959
}
6060

@@ -70,15 +70,15 @@ public void setAdapter(ArrayAdapter adapter) {
7070
Enum to hold the constants that users can pick to determine what the borders of the
7171
data printed to the console look like.
7272
*/
73-
enum LayoutType {
73+
enum Formatter {
7474
DASH("\n---------------------\n", "|"),
7575
CIRCLE("\no 0 o 0 o 0 o 0 o 0 o\n", "0"),
7676
STAR("\n*********************\n", "*");
7777

7878
String horizontalBorder, verticalBorder;
7979

8080
// Constructor
81-
private LayoutType(String horizontalBorder, String verticalBorder) {
81+
private Formatter(String horizontalBorder, String verticalBorder) {
8282
this.horizontalBorder = horizontalBorder;
8383
this.verticalBorder = verticalBorder;
8484
}
@@ -92,11 +92,11 @@ type to show (the enum). It then iterates over the array and prints out the appr
9292
class ArrayAdapter {
9393

9494
private Student[] studentArray;
95-
private LayoutType layoutType;
95+
private Formatter formatter;
9696

97-
public ArrayAdapter(Student[] studentArray, LayoutType layoutType) {
97+
public ArrayAdapter(Student[] studentArray, Formatter formatter) {
9898
this.studentArray = studentArray;
99-
this.layoutType = layoutType;
99+
this.formatter = formatter;
100100
}
101101

102102
public String createRow(int index) {
@@ -107,8 +107,8 @@ public String createRow(int index) {
107107
Student currentStudent = studentArray[index];
108108

109109
// Determine layout type
110-
horizontalBorder = layoutType.horizontalBorder;
111-
verticalBorder = layoutType.verticalBorder;
110+
horizontalBorder = formatter.horizontalBorder;
111+
verticalBorder = formatter.verticalBorder;
112112

113113
row = horizontalBorder + verticalBorder + " " + currentStudent.getName() + " " + horizontalBorder;
114114
return row;
@@ -140,9 +140,9 @@ public static void main(String[] args) {
140140
};
141141

142142
// Constructing the adapter and starting it
143-
ArrayAdapter adapter = new ArrayAdapter(students, LayoutType.DASH);
144-
ListContainer container = new ListContainer(12);
143+
ArrayAdapter adapter = new ArrayAdapter(students, Formatter.DASH);
144+
Printer printer = new Printer(12);
145145

146-
container.setAdapter(adapter);
146+
printer.setAdapter(adapter);
147147
}
148148
}

Week3/Lab/.cs50.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ lab50:
66
files:
77
- !exclude "*"
88
- !include Student.java
9-
- !include LayoutType.java
10-
- !include ListContainer.java
9+
- !include Printer.java
1110
- !include ArrayAdapter.java
1211
- !include AdapterTest.java
13-
- !include ArrayListTest.java

Week3/Lab/AdapterTest.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
/*
2-
The class containing the main method.
3-
*/
1+
import java.util.ArrayList;
2+
43
public class AdapterTest {
54

6-
// The main method is the start of the program
7-
public static void main() {
5+
public static void main(String[] args) {
86

9-
// Run the code in ArrayListTest and the list inside
10-
ArrayListTest.main();
11-
System.out.println("List Length:" ArrayListTest.list.size());
7+
ArrayList<Student> list = Student.createSampleList();
8+
System.out.println("Number of students: " + list.size());
129

1310
}
14-
}
11+
}

Week3/Lab/ArrayAdapter.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,21 @@
11
import java.util.ArrayList;
22

3-
/*
4-
...
5-
*/
63
public class ArrayAdapter {
74

85
private ArrayList<Student> studentList;
9-
private LayoutType layoutType;
106

11-
/*
12-
...
13-
*/
14-
public ArrayAdapter(ArrayList<Student> studentList, LayoutType layoutType) {
7+
public ArrayAdapter(ArrayList<Student> studentList) {
158
this.studentList = studentList;
16-
this.layoutType = layoutType;
179
}
1810

19-
/*
20-
...
21-
*/
2211
public String createRow(int index) {
2312

24-
String row;
25-
String horizontalBorder, verticalBorder;
26-
2713
Student currentStudent = studentList.get(index);
28-
29-
horizontalBorder = layoutType.horizontalBorder;
30-
verticalBorder = layoutType.verticalBorder;
31-
32-
row = horizontalBorder + verticalBorder + " " + currentStudent.getName() + " " + horizontalBorder;
14+
String row = "| " + currentStudent.getName() + " |";
3315
return row;
3416
}
3517

36-
/*
37-
...
38-
*/
3918
public int getItemCount() {
4019
return studentList.size();
4120
}
42-
}
21+
}

Week3/Lab/ArrayListTest.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

Week3/Lab/LayoutType.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

Week3/Lab/ListContainer.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)