+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/.idea/misc.xml b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/.idea/misc.xml
index 3d21ecd..5d19981 100644
--- a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/.idea/misc.xml
+++ b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/.idea/misc.xml
@@ -37,7 +37,7 @@
-
+
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/AndroidManifest.xml b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/AndroidManifest.xml
index 81d417a..0a79aa2 100644
--- a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/AndroidManifest.xml
+++ b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/AndroidManifest.xml
@@ -1,6 +1,6 @@
+ package="com.example.yang.mylists">
-
+
-
+
+
+
\ No newline at end of file
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/Candidate.java b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/Candidate.java
new file mode 100644
index 0000000..1e20727
--- /dev/null
+++ b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/Candidate.java
@@ -0,0 +1,34 @@
+package com.example.yang.mylists;
+
+/**
+ * Created by jianhuayang on 30/10/2017.
+ */
+
+public class Candidate {
+ private String name;
+ private String detail;
+ private int photo;
+
+ public Candidate(String name, String detail, int photo) {
+ this.name = name;
+ this.detail = detail;
+ this.photo = photo;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getDetail() {
+ return detail;
+ }
+
+ public int getPhoto() {
+ return photo;
+ }
+
+ @Override
+ public String toString() {
+ return detail;
+ }
+}
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/CandidateAdapter.java b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/CandidateAdapter.java
new file mode 100644
index 0000000..10b686a
--- /dev/null
+++ b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/CandidateAdapter.java
@@ -0,0 +1,56 @@
+package com.example.yang.mylists;
+
+import android.content.Context;
+import android.support.annotation.NonNull;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+import java.util.ArrayList;
+
+/**
+ * Created by jianhuayang on 30/10/2017.
+ */
+
+public class CandidateAdapter extends ArrayAdapter {
+
+ private int resource;
+ private ArrayList candidates;
+ private Context context;
+
+ public CandidateAdapter(Context context, int resource, ArrayList candidates) {
+ super(context, resource, candidates);
+ this.resource = resource;
+ this.candidates = candidates;
+ this.context = context;
+ }
+
+ @NonNull
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ View v = convertView;
+ try {
+ if (v == null) {
+ LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ v = layoutInflater.inflate(resource, parent, false);
+ }
+
+ ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
+ TextView textViewName = (TextView) v.findViewById(R.id.textViewName);
+ TextView textViewDetail = (TextView) v.findViewById(R.id.textViewDetail);
+
+ imageView.setImageResource(candidates.get(position).getPhoto());
+ textViewName.setText(candidates.get(position).getName());
+ textViewDetail.setText(candidates.get(position).getDetail());
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ e.getCause();
+ }
+ return v;
+ }
+
+}
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/MainActivity.java b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/MainActivity.java
index 7ef348b..2bc4b9e 100644
--- a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/MainActivity.java
+++ b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/MainActivity.java
@@ -1,7 +1,8 @@
package com.example.yang.mylists;
-import android.support.v7.app.AppCompatActivity;
+import android.content.Intent;
import android.os.Bundle;
+import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
@@ -42,4 +43,8 @@ public class MainActivity extends AppCompatActivity {
arrayAdapterNew.add("New Someone");
arrayAdapter.notifyDataSetInvalidated();
}
+ public void onButtonClick(View v){
+ startActivity(new Intent(this, PhotoListActivity.class));
+ }
+
}
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/PhotoListActivity.java b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/PhotoListActivity.java
new file mode 100644
index 0000000..d67fc31
--- /dev/null
+++ b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/java/com/example/yang/mylists/PhotoListActivity.java
@@ -0,0 +1,59 @@
+package com.example.yang.mylists;
+
+import android.os.Bundle;
+import android.support.v7.app.AppCompatActivity;
+import android.view.View;
+import android.widget.AdapterView;
+import android.widget.ListView;
+import android.widget.Toast;
+
+import java.util.ArrayList;
+
+public class PhotoListActivity extends AppCompatActivity {
+
+ private ListView listView;
+ private String[] candidateNames;
+ private String[] candidateDetails;
+ public static int[] candidatePhotos = {
+ R.drawable.clinton,
+ R.drawable.sanders,
+ R.drawable.omalley,
+ R.drawable.chafee,
+ R.drawable.trump,
+ R.drawable.carson,
+ R.drawable.rubio,
+ R.drawable.bush
+ };
+ private ArrayList candidates = new ArrayList<>();
+
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_photo_list);
+
+ candidateNames = getResources().getStringArray(R.array.candidateNames);
+ candidateDetails = getResources().getStringArray(R.array.candidateDetails);
+ generateCandidates();
+
+ listView = (ListView) findViewById(R.id.listViewComplex);
+ listView.setAdapter(new CandidateAdapter(this, R.layout.list_item, candidates));
+ listView.setOnItemClickListener(
+
+ new AdapterView.OnItemClickListener() {
+ @Override
+ public void onItemClick(AdapterView> parent, View view, int position, long id) {
+
+ Toast.makeText(getBaseContext(), "You clicked " + candidates.get(position), Toast.LENGTH_SHORT).show();
+ }
+ }
+ );
+ }
+
+ private void generateCandidates() {
+
+ for (int i = 0; i < candidatePhotos.length; i++) {
+ candidates.add(new Candidate(candidateNames[i], candidateDetails[i], candidatePhotos[i]));
+ }
+ }
+}
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/bush.jpg b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/bush.jpg
new file mode 100644
index 0000000..76a5c24
Binary files /dev/null and b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/bush.jpg differ
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/carson.jpg b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/carson.jpg
new file mode 100644
index 0000000..535a2ea
Binary files /dev/null and b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/carson.jpg differ
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/chafee.jpg b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/chafee.jpg
new file mode 100644
index 0000000..a2f04c1
Binary files /dev/null and b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/chafee.jpg differ
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/clinton.jpg b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/clinton.jpg
new file mode 100644
index 0000000..ab248f3
Binary files /dev/null and b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/clinton.jpg differ
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/omalley.jpg b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/omalley.jpg
new file mode 100644
index 0000000..074db96
Binary files /dev/null and b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/omalley.jpg differ
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/rubio.jpg b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/rubio.jpg
new file mode 100644
index 0000000..5574335
Binary files /dev/null and b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/rubio.jpg differ
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/sanders.jpg b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/sanders.jpg
new file mode 100644
index 0000000..772b27f
Binary files /dev/null and b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/sanders.jpg differ
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/trump.jpg b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/trump.jpg
new file mode 100644
index 0000000..764d0bf
Binary files /dev/null and b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/drawable/trump.jpg differ
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/layout/activity_main.xml b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/layout/activity_main.xml
index 2592b41..ca93007 100644
--- a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/layout/activity_main.xml
+++ b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/layout/activity_main.xml
@@ -1,21 +1,36 @@
+ xmlns:app="http://schemas.android.com/apk/res-auto"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ tools:context="com.example.yang.mylists.MainActivity">
+
+
+ app:layout_constraintTop_toBottomOf="@+id/complexList"
+ app:layout_constraintVertical_bias="0.0"/>
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/layout/activity_photo_list.xml b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/layout/activity_photo_list.xml
new file mode 100644
index 0000000..2bc0983
--- /dev/null
+++ b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/layout/activity_photo_list.xml
@@ -0,0 +1,37 @@
+
+
+
+
+
+
+
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/layout/list_item.xml b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/layout/list_item.xml
new file mode 100644
index 0000000..c6df075
--- /dev/null
+++ b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/MyLists/app/src/main/res/layout/list_item.xml
@@ -0,0 +1,39 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/README.md b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/README.md
index 5f56e8e..388430d 100644
--- a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/README.md
+++ b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/README.md
@@ -96,14 +96,14 @@ If you run the app, what you'll see is something like this:
The following code can be added after the `setOnItemClickListener()` block, but still within `onCreate()`method:
- ```java
- List candidateNamesNew = new ArrayList(Arrays.asList(candidateNames));
- ArrayAdapter arrayAdapterNew = new ArrayAdapter(this, android.R.layout
- .simple_list_item_1, candidateNamesNew);
- listView.setAdapter(arrayAdapterNew);
- arrayAdapterNew.add("New Someone");
- arrayAdapter.notifyDataSetInvalidated();
- ```
+```java
+List candidateNamesNew = new ArrayList(Arrays.asList(candidateNames));
+ArrayAdapter arrayAdapterNew = new ArrayAdapter(this, android.R.layout
+ .simple_list_item_1, candidateNamesNew);
+listView.setAdapter(arrayAdapterNew);
+arrayAdapterNew.add("New Someone");
+arrayAdapter.notifyDataSetInvalidated();
+```
Have a look at the code above, and try to answer the following questions:
@@ -115,7 +115,7 @@ Have a look at the code above, and try to answer the following questions:
Simple ListView is useful for displaying data that can be converted to strings in easy steps. But if you want to have fine control of the presentation of single entries in your ListView, you need to provide customized layout files for your adapter. In this way, you'll make it a 'complex ListView'.
-1. First of all, download some images to use later on. Click on [this link](https://github.com/covcom/300CEM/blob/master/Week_05_AdapterViews_and_Fragments/.md_images/candidates_photos.zip) to go to GitHub page and then click 'View Raw' to download some images of US presidential election candidates. Add those to your res/drawable resources folder.
+1. First of all, download some images to use later on. Click on [this link](https://github.coventry.ac.uk/300CEM-1718SEPJAN/TEACHING-MATERIALS/blob/master/Additional_resources/candidates_photos.zip) to go to GitHub page and then click 'View Raw' to download some images of US presidential election candidates. Add those to your res/drawable resources folder.
2. Create a new Java class called Candidate and insert the following
@@ -204,18 +204,29 @@ Simple ListView is useful for displaying data that can be converted to strings i
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
+ android:layout_marginLeft="8dp"
+ android:layout_marginStart="8dp"
+ android:layout_marginTop="8dp"
android:text="2016 Presidential Candidates"
- android:textAppearance="?android:attr/textAppearanceLarge" />
+ android:textAppearance="?android:attr/textAppearanceLarge"
+ app:layout_constraintLeft_toLeftOf="parent"
+ app:layout_constraintTop_toTopOf="parent"/>
+ android:layout_marginLeft="16dp"
+ android:layout_marginRight="8dp"
+ android:layout_marginStart="8dp"
+ android:layout_marginTop="7dp"
+ app:layout_constraintLeft_toLeftOf="parent"
+ app:layout_constraintRight_toRightOf="parent"
+ app:layout_constraintTop_toBottomOf="@+id/textView"/>
```
- This layout is very similar to the one we saw earlier in the simple list example. The only difference is that it has an additional TextView above ListView.
+ This layout is very similar to the simple list example. The only difference is that it has an additional TextView above ListView.
5. In PhotoListActivity.java, add the following class member variable declaration/initialization.
@@ -236,9 +247,9 @@ Simple ListView is useful for displaying data that can be converted to strings i
private ArrayList candidates = new ArrayList<>();
```
- One thing to note in the code above is that an array of integers is declared for drawable resources. The type is int, why?
+ Note in the code above an array of integers is declared for drawable resources. (Why is the type int?)
-6. Insert the following into the `onCreate()` method, just below `setContentView()`, to initialize variables
+6. Insert String array initialization into the `onCreate()` method, just below `setContentView()`, so your `onCreate()` becomes
```java
@Override
@@ -251,7 +262,7 @@ Simple ListView is useful for displaying data that can be converted to strings i
generateCandidates();
```
- Insert the following code at outside of `onCreate()` but inside the class. This is to create a function that initializes the ArrayList for Candidate class.
+ Insert the following as a class method. This is to create a function that initializes the ArrayList for Candidate class.
```java
private void generateCandidates() {
@@ -327,7 +338,7 @@ Simple ListView is useful for displaying data that can be converted to strings i
}
```
- It's important to understand the code above: our CandidatesAdapter class extends ArrayAdapter of type Candidates. In the constructor, we provide the layout resource name i.e. the file that contains the customized layout XML created earlier. The most important method is `getView()`, where we check if a convertView (i.e. old view) exists or not. If it doesn’t, we’ll need to inflate it. The reason we do this is because ListView recycles its rows when they move out of the screen, instead of creating new ones, to save system resources.
+ It's important to understand the code above: the CandidatesAdapter class extends ArrayAdapter of type Candidates. In the constructor, we provide the layout resource name i.e. the file that contains the customized layout XML created earlier. The most important method is `getView()`, where we check if a convertView (i.e. old view) exists or not. If it doesn’t, we’ll need to inflate it. The reason we do this is because ListView recycles its rows when they move out of the screen, instead of creating new ones, to save system resources.
> [How ListView's recycling mechanism works](http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works)
@@ -347,11 +358,15 @@ Simple ListView is useful for displaying data that can be converted to strings i
android:id="@+id/complexList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
+ android:layout_marginLeft="8dp"
+ android:layout_marginTop="8dp"
android:onClick="onButtonClick"
- android:text="Complex List" />
+ android:text="Complex List"
+ app:layout_constraintLeft_toLeftOf="parent"
+ app:layout_constraintTop_toTopOf="parent"/>
```
- Insert the following attribute into the ListView `android:layout_below="@id/complexList"`, and remove attribute `android:layout_alignParentTop="true"` if it is in there.
+ You'll see that ListView and Button overlap at the top-left corner of the screen. Change the following attribute of `ListView` to have more space at the top `app:layout_constraintTop_toBottomOf="@+id/complexList"`
10. Open MainActivity.java, insert the following into the class
@@ -361,140 +376,27 @@ Simple ListView is useful for displaying data that can be converted to strings i
}
```
-Steps 9 & 10 are needed only for the sake of demonstration. If you run the app, click on 'Complex List' button, you'll see the following:
+If you run the app, click on 'Complex List' button, you'll see the following:
![photo](.md_images/photo.png)
-### Grid View
-
-
-Previous two examples used default ArrayAdapter and customized (extended) ArrayAdapter. In fact, ArrayAdapter is a subclass of BaseAdapter. Let's now go one step further and see how to customize (extend) BaseAdapter. In the following example, you'll learn how to create a GridView and provide a customized BaseAdaptor for it.
-
-1. Create a new Activity using the 'Empty Activity' template and name it GridAcitivty. Open activity_grid.xml and change the container layout from RelativeLayout to GridView. Insert the following attributes into the opening tag of GridView:
-
- ```xml
- android:columnWidth="90dp"
- android:gravity="center"
- android:horizontalSpacing="10dp"
- android:numColumns="auto_fit"
- android:stretchMode="columnWidth"
- android:verticalSpacing="10dp"
- ```
-
- There are new attributes, but they are quite self-explanatory.
-
-2. Open GridActivity.java, insert the following code into `onCreate()` just before the ending curly brackets '}'.
-
- ```java
- GridView gridview = (GridView) findViewById(R.id.activity_grid);
- gridview.setAdapter(new ImageAdapter(this));
-
- gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- public void onItemClick(AdapterView> parent, View v, int position, long id) {
- Toast.makeText(getBaseContext(), "At position " + position + " is " + getResources().getStringArray(R.array.candidateNames)[position], Toast.LENGTH_SHORT).show();
- }
- });
- ```
- The only thing new here is the ImageAdapter, which we'll look at next.
-
-3. Create a new class called ImageAdapter. Replace the auto-generated class with the following
-
- ```java
- public class ImageAdapter extends BaseAdapter {
-
- private int[] candidatePhotos = PhotoListActivity.candidatePhotos;
- private Context context;
-
- public ImageAdapter(Context context) {
- this.context = context;
- }
-
- @Override
- public int getCount() {
- return candidatePhotos.length;
- }
-
- @Override
- public Object getItem(int position) {
- return null;
- }
-
- @Override
- public long getItemId(int position) {
- return 0;
- }
-
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageView;
- if (convertView == null) {
- imageView = new ImageView(context);
- imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
- imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
- imageView.setPadding(8, 8, 8, 8);
- } else {
- imageView = (ImageView) convertView;
- }
-
- imageView.setImageResource(candidatePhotos[position]);
- return imageView;
- }
-
- }
- ```
-
- BaseAdapter is the superclass of different Adapters, but it's an abstract class. That means you have to provide concrete implementations for all abstract methods. That's why you see `getItem()` and `getItemId()` above. Although we don't use those two methods in our example we have to implement them. `getCount()` return the size of the data. The most important one is still `getView()`. Here instead of using layout resource file, we programmatically add layout widgets into the parent and set it's properties. `GridView.LayoutParams()` creates an GridView.LayoutParams object that resides inside the parent view, which is a GridView in our case.
-
-5. Insert the following into activity_main.xml, just before the ListView
-
- ```xml
-
- ```
-
- Change the ListView attribute `android:layout_below` value from `"@id/complexList"` to `"@id/gridView"`.
-
-10. Open MainActivity.java, insert the following into the class
-
- ```java
- public void onButtonClick(View v){
- startActivity(new Intent(this, PhotoListActivity.class));
- }
- ```
-6. Open MainActivity.java, replace contents of `onButtonClick()` with the following
-
- ```java
- switch (v.getId()) {
- case R.id.complexList:
- startActivity(new Intent(this, PhotoListActivity.class));
- break;
- case R.id.gridView:
- startActivity(new Intent(this, GridActivity.class));
- break;
- }
- ```
-
-If you run the app and click on 'GridView', you'll see something similar to below
-
-![grid](.md_images/grid.png)
+
### AdapterView hierarchy
-You saw BaseAdapter and ArrayAdapter already. If you Google online examples you'll see more Adapters such as ListAdapter and SimpleAdapter etc. What are the relationships among these?
+You saw ArrayAdapter already. If you Google online examples you'll see more Adapters such as BaseAdapter, ListAdapter, and SimpleAdapter etc. What are the relationships among these?
Basically, ArrayAdapter is the first concrete Adapter in the tree, above it are interfaces and an abstract class. But sometimes people do declare something like `ListAdapter listAdapter = new ArrayAdapter()`, don't be confused.
-![AdapterViewHierarchy](http://www.intertech.com/Blog/wp-content/uploads/2014/06/HeirarchyOfAdapter-480x396.png)
+
+![AdapterViewHierarchy](.md_images/HeirarchyOfAdapter.png)
A similar hierarchy can be drawn for AdapterView and subclasses. Even though those 'collection' Views are named differently, they are in fact closely related to each other.
-![HeirarchyOfAdapter](http://www.intertech.com/Blog/wp-content/uploads/2014/06/AdapterViewHierarchy-480x396.png)
+
+![HeirarchyOfAdapter](.md_images/AdapterViewHierarchy.png)
-> Above images from a [blog](http://www.intertech.com/Blog/android-adapters-adapterviews/) written by Jim White.
+> Above images are from a [blog](http://www.intertech.com/Blog/android-adapters-adapterviews/) written by Jim White.
## Lab 2 Fragments
diff --git a/old(needs_updating)/Week_05_AdapterViews_and_Fragments/README_gridview.md b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/README_gridview.md
new file mode 100644
index 0000000..0ecd8d9
--- /dev/null
+++ b/old(needs_updating)/Week_05_AdapterViews_and_Fragments/README_gridview.md
@@ -0,0 +1,116 @@
+### Grid View
+
+
+Previous two examples used default ArrayAdapter and customized (extended) ArrayAdapter. In fact, ArrayAdapter is a subclass of BaseAdapter. Let's now go one step further and see how to customize (extend) BaseAdapter. In the following example, you'll learn how to create a GridView and provide a customized BaseAdaptor for it.
+
+1. Create a new Activity using the 'Empty Activity' template and name it GridAcitivty. Open activity_grid.xml and change the container layout from RelativeLayout to GridView. Insert the following attributes into the opening tag of GridView:
+
+ ```xml
+ android:columnWidth="90dp"
+ android:gravity="center"
+ android:horizontalSpacing="10dp"
+ android:numColumns="auto_fit"
+ android:stretchMode="columnWidth"
+ android:verticalSpacing="10dp"
+ ```
+
+ There are new attributes, but they are quite self-explanatory.
+
+2. Open GridActivity.java, insert the following code into `onCreate()` just before the ending curly brackets '}'.
+
+ ```java
+ GridView gridview = (GridView) findViewById(R.id.activity_grid);
+ gridview.setAdapter(new ImageAdapter(this));
+
+ gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
+ public void onItemClick(AdapterView> parent, View v, int position, long id) {
+ Toast.makeText(getBaseContext(), "At position " + position + " is " + getResources().getStringArray(R.array.candidateNames)[position], Toast.LENGTH_SHORT).show();
+ }
+ });
+ ```
+ The only thing new here is the ImageAdapter, which we'll look at next.
+
+3. Create a new class called ImageAdapter. Replace the auto-generated class with the following
+
+ ```java
+ public class ImageAdapter extends BaseAdapter {
+
+ private int[] candidatePhotos = PhotoListActivity.candidatePhotos;
+ private Context context;
+
+ public ImageAdapter(Context context) {
+ this.context = context;
+ }
+
+ @Override
+ public int getCount() {
+ return candidatePhotos.length;
+ }
+
+ @Override
+ public Object getItem(int position) {
+ return null;
+ }
+
+ @Override
+ public long getItemId(int position) {
+ return 0;
+ }
+
+ public View getView(int position, View convertView, ViewGroup parent) {
+ ImageView imageView;
+ if (convertView == null) {
+ imageView = new ImageView(context);
+ imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
+ imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
+ imageView.setPadding(8, 8, 8, 8);
+ } else {
+ imageView = (ImageView) convertView;
+ }
+
+ imageView.setImageResource(candidatePhotos[position]);
+ return imageView;
+ }
+
+ }
+ ```
+
+ BaseAdapter is the superclass of different Adapters, but it's an abstract class. That means you have to provide concrete implementations for all abstract methods. That's why you see `getItem()` and `getItemId()` above. Although we don't use those two methods in our example we have to implement them. `getCount()` return the size of the data. The most important one is still `getView()`. Here instead of using layout resource file, we programmatically add layout widgets into the parent and set it's properties. `GridView.LayoutParams()` creates an GridView.LayoutParams object that resides inside the parent view, which is a GridView in our case.
+
+5. Insert the following into activity_main.xml, just before the ListView
+
+ ```xml
+
+ ```
+
+ Change the ListView attribute `android:layout_below` value from `"@id/complexList"` to `"@id/gridView"`.
+
+10. Open MainActivity.java, insert the following into the class
+
+ ```java
+ public void onButtonClick(View v){
+ startActivity(new Intent(this, PhotoListActivity.class));
+ }
+ ```
+6. Open MainActivity.java, replace contents of `onButtonClick()` with the following
+
+ ```java
+ switch (v.getId()) {
+ case R.id.complexList:
+ startActivity(new Intent(this, PhotoListActivity.class));
+ break;
+ case R.id.gridView:
+ startActivity(new Intent(this, GridActivity.class));
+ break;
+ }
+ ```
+
+If you run the app and click on 'GridView', you'll see something similar to below
+
+![grid](.md_images/grid.png)
\ No newline at end of file