Skip to content

Commit dfcb00c

Browse files
author
Sébastien Vitard
committed
Step-1 finished
1 parent d0e1f5a commit dfcb00c

7 files changed

Lines changed: 141 additions & 19 deletions

File tree

‎app/build.gradle‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ android {
1919

2020
dependencies {
2121
compile 'com.android.support:appcompat-v7:26.1.0'
22-
// TODO: add volley library 'com.android.volley:volley:1.0.0'
22+
compile 'com.android.volley:volley:1.0.0'
2323
}

‎app/src/main/AndroidManifest.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="io.kristal.events">
44

5-
<!-- TODO: add Internet permission -->
5+
<uses-permission android:name="android.permission.INTERNET" />
66

77
<application
88
android:allowBackup="true"

‎app/src/main/java/io/kristal/events/main/MainActivity.java‎

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
import android.os.Bundle;
44
import android.support.v7.app.AppCompatActivity;
5+
import android.util.Log;
6+
import android.widget.ListView;
7+
import android.widget.Toast;
8+
9+
import com.android.volley.RequestQueue;
10+
import com.android.volley.Response;
11+
import com.android.volley.VolleyError;
12+
import com.android.volley.toolbox.JsonArrayRequest;
13+
import com.android.volley.toolbox.Volley;
14+
15+
import org.json.JSONArray;
516

617
import java.util.ArrayList;
718

@@ -13,15 +24,19 @@ public final class MainActivity extends AppCompatActivity {
1324

1425
private static String TAG = MainActivity.class.getSimpleName();
1526

27+
private MainAdapter mAdapter;
28+
private RequestQueue mRequestQueue;
29+
1630
@Override
1731
protected void onCreate(Bundle savedInstanceState) {
1832
super.onCreate(savedInstanceState);
1933

20-
// TODO: init adapter with Constructor(Context, ResId, ResId)
34+
mAdapter = new MainAdapter(this, R.layout.row_main, R.id.text_title);
2135

2236
setContentView(R.layout.activity_main);
2337

24-
// TODO: retrieve ListView & set adapter
38+
ListView listView = findViewById(android.R.id.list);
39+
listView.setAdapter(mAdapter);
2540

2641
if (savedInstanceState == null) {
2742
reset();
@@ -32,12 +47,38 @@ protected void onCreate(Bundle savedInstanceState) {
3247
}
3348

3449
private void reset() {
35-
// TODO: init RequestQueue if null, then add request to "http://cobaltians.org/events.json",
36-
// save events with `EventsList.setAll(Context, JSONArray)` & call `reload()` method
50+
if (mRequestQueue == null) {
51+
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
52+
}
53+
54+
mRequestQueue.add(new JsonArrayRequest("http://cobaltians.org/events.json",
55+
new Response.Listener<JSONArray>() {
56+
@Override
57+
public void onResponse(JSONArray response) {
58+
EventsList.setAll(MainActivity.this, response);
59+
reload();
60+
61+
Toast.makeText(MainActivity.this, R.string.reset_success,
62+
Toast.LENGTH_LONG).show();
63+
}
64+
},
65+
new Response.ErrorListener() {
66+
@Override
67+
public void onErrorResponse(VolleyError error) {
68+
Toast.makeText(MainActivity.this, R.string.reset_error,
69+
Toast.LENGTH_LONG).show();
70+
71+
Log.e(TAG, "reset: " + error.getMessage());
72+
error.printStackTrace();
73+
}
74+
}));
3775
}
3876

3977
private void reload() {
4078
ArrayList<Event> events = EventsList.getAll();
41-
// TODO: reload adapter data
79+
if (! mAdapter.isEmpty()) {
80+
mAdapter.clear();
81+
}
82+
mAdapter.addAll(events);
4283
}
4384
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.kristal.events.main;
2+
3+
import android.content.Context;
4+
import android.support.annotation.NonNull;
5+
import android.support.annotation.Nullable;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.ArrayAdapter;
9+
import android.widget.TextView;
10+
11+
import java.text.DateFormat;
12+
13+
import io.kristal.events.R;
14+
import io.kristal.events.model.Event;
15+
16+
/**
17+
* Created by sebastien on 10/01/2018.
18+
*/
19+
20+
final class MainAdapter extends ArrayAdapter<Event> {
21+
22+
private static DateFormat sDateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
23+
24+
MainAdapter(@NonNull Context context, int resource, int textViewResourceId) {
25+
super(context, resource, textViewResourceId);
26+
}
27+
28+
@NonNull
29+
@Override
30+
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
31+
View view = super.getView(position, convertView, parent);
32+
33+
TextView titleTextView = view.findViewById(R.id.text_title);
34+
TextView placeTextView = view.findViewById(R.id.text_place);
35+
TextView dateTextView = view.findViewById(R.id.text_date);
36+
37+
Event event = getItem(position);
38+
if (event != null) {
39+
titleTextView.setText(event.getTitle());
40+
placeTextView.setText(event.getPlace());
41+
dateTextView.setText(sDateFormat.format(event.getDate()));
42+
}
43+
44+
return view;
45+
}
46+
}
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<!-- TODO: replace with ListView -->
3-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
43
xmlns:tools="http://schemas.android.com/tools"
4+
android:id="@android:id/list"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7-
tools:context=".main.MainActivity">
8-
9-
<TextView
10-
android:layout_width="wrap_content"
11-
android:layout_height="wrap_content"
12-
android:layout_gravity="center"
13-
android:text="@string/hello_world" />
14-
15-
</FrameLayout>
7+
tools:context=".main.MainActivity"/>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:padding="8dp"
7+
android:weightSum="1.0"
8+
tools:context=".main.MainActivity">
9+
10+
<LinearLayout
11+
android:layout_width="0dp"
12+
android:layout_height="wrap_content"
13+
android:layout_weight="1.0"
14+
android:orientation="vertical">
15+
16+
<TextView
17+
android:id="@+id/text_title"
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content"
20+
android:textStyle="bold"
21+
android:text="@string/row_title_placeholder" />
22+
23+
<TextView
24+
android:id="@+id/text_place"
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:text="@string/row_place_placeholder" />
28+
29+
</LinearLayout>
30+
31+
<TextView
32+
android:id="@+id/text_date"
33+
android:layout_width="wrap_content"
34+
android:layout_height="wrap_content"
35+
android:layout_gravity="center_vertical"
36+
android:text="@string/row_date_placeholder" />
37+
38+
</LinearLayout>

‎app/src/main/res/values/strings.xml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
<string name="app_label">Events</string>
44
<string name="hello_world">Hello World</string>
55

6-
<!-- TODO: Add reset_success/error, row_title/date/place_placeholder strings -->
6+
<string name="reset_success">Events reset</string>
7+
<string name="reset_error">Error while retrieving default events</string>
8+
9+
<string name="row_title_placeholder">Turing birthday</string>
10+
<string name="row_date_placeholder">06/23/1912</string>
11+
<string name="row_place_placeholder">London</string>
712

813
</resources>

0 commit comments

Comments
 (0)