From 24252ea9fcd7430c1b02323fddba983bf64bd677 Mon Sep 17 00:00:00 2001 From: Tein van der Lugt Date: Sat, 20 Feb 2016 19:20:47 +0100 Subject: [PATCH] Create AllPeopleActivity --- app/src/main/AndroidManifest.xml | 4 + .../android/greekgods/AllPeopleActivity.java | 85 +++++++++++++++++++ .../AllPeopleRecyclerViewAdapter.java | 66 ++++++++++++++ .../android/greekgods/MainActivity.java | 18 +++- .../android/greekgods/models/Person.java | 9 ++ .../main/res/layout/activity_all_people.xml | 27 ++++++ app/src/main/res/layout/activity_main.xml | 11 ++- .../main/res/layout/list_item_all_people.xml | 30 +++++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/values/styles.xml | 7 +- 10 files changed, 250 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/teinvdlugt/android/greekgods/AllPeopleActivity.java create mode 100644 app/src/main/java/com/teinvdlugt/android/greekgods/AllPeopleRecyclerViewAdapter.java create mode 100644 app/src/main/res/layout/activity_all_people.xml create mode 100644 app/src/main/res/layout/list_item_all_people.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 85d82d3..7d44bd0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,6 +18,10 @@ + + diff --git a/app/src/main/java/com/teinvdlugt/android/greekgods/AllPeopleActivity.java b/app/src/main/java/com/teinvdlugt/android/greekgods/AllPeopleActivity.java new file mode 100644 index 0000000..6a56d69 --- /dev/null +++ b/app/src/main/java/com/teinvdlugt/android/greekgods/AllPeopleActivity.java @@ -0,0 +1,85 @@ +package com.teinvdlugt.android.greekgods; + +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.os.AsyncTask; +import android.os.Bundle; +import android.os.PersistableBundle; +import android.support.design.widget.Snackbar; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.Toolbar; +import android.view.MenuItem; + +import com.teinvdlugt.android.greekgods.models.Person; + +import java.util.ArrayList; +import java.util.List; + +public class AllPeopleActivity extends AppCompatActivity implements AllPeopleRecyclerViewAdapter.OnPersonClickListener { + + private RecyclerView recyclerView; + private AllPeopleRecyclerViewAdapter adapter; + + @SuppressWarnings("ConstantConditions") + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_all_people); + setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + recyclerView = (RecyclerView) findViewById(R.id.recyclerView); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + adapter = new AllPeopleRecyclerViewAdapter(this, new ArrayList(), this); + recyclerView.setAdapter(adapter); + refresh(); + } + + private void refresh() { + new AsyncTask>() { + @Override + protected List doInBackground(Void... params) { + SQLiteDatabase db = openOrCreateDatabase("data", 0, null); + String[] columns = {"personId", "name"}; + Cursor c = db.query("people", columns, null, null, null, null, "name"); + int idColumn = c.getColumnIndex("personId"); + int nameColumn = c.getColumnIndex("name"); + + List result = new ArrayList<>(); + + c.moveToFirst(); + do { + Person p = new Person(); + p.setId(c.getInt(idColumn)); + p.setName(c.getString(nameColumn)); + result.add(p); + } while (c.moveToNext()); + c.close(); + db.close(); + + return result; + } + + @Override + protected void onPostExecute(List persons) { + adapter.setData(persons); + } + }.execute(); + } + + @Override + public void onClickPerson(Person person) { + Snackbar.make(recyclerView, "Well done, you clicked " + person.getName() + "'s name!", Snackbar.LENGTH_LONG).show(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } + return false; + } +} diff --git a/app/src/main/java/com/teinvdlugt/android/greekgods/AllPeopleRecyclerViewAdapter.java b/app/src/main/java/com/teinvdlugt/android/greekgods/AllPeopleRecyclerViewAdapter.java new file mode 100644 index 0000000..c237fac --- /dev/null +++ b/app/src/main/java/com/teinvdlugt/android/greekgods/AllPeopleRecyclerViewAdapter.java @@ -0,0 +1,66 @@ +package com.teinvdlugt.android.greekgods; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.teinvdlugt.android.greekgods.models.Person; + +import java.util.List; + +public class AllPeopleRecyclerViewAdapter extends RecyclerView.Adapter { + + private Context context; + private List data; + private OnPersonClickListener onPersonClickListener; + + public AllPeopleRecyclerViewAdapter(Context context, List data, OnPersonClickListener onPersonClickListener) { + this.context = context; + this.data = data; + this.onPersonClickListener = onPersonClickListener; + } + + public interface OnPersonClickListener { + void onClickPerson(Person person); + } + + public void setData(List data) { + this.data = data; + notifyDataSetChanged(); + } + + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item_all_people, parent, false)); + } + + @Override + public void onBindViewHolder(final ViewHolder holder, final int position) { + holder.nameTextView.setText(data.get(position).getName()); + holder.itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (onPersonClickListener != null) + onPersonClickListener.onClickPerson(data.get(holder.getAdapterPosition())); + } + }); + } + + @Override + public int getItemCount() { + return data.size(); + } + + static class ViewHolder extends RecyclerView.ViewHolder { + private TextView nameTextView; + + public ViewHolder(View itemView) { + super(itemView); + + nameTextView = (TextView) itemView.findViewById(R.id.name_textView); + } + } +} diff --git a/app/src/main/java/com/teinvdlugt/android/greekgods/MainActivity.java b/app/src/main/java/com/teinvdlugt/android/greekgods/MainActivity.java index d413fb0..45e4af1 100644 --- a/app/src/main/java/com/teinvdlugt/android/greekgods/MainActivity.java +++ b/app/src/main/java/com/teinvdlugt/android/greekgods/MainActivity.java @@ -17,6 +17,7 @@ package com.teinvdlugt.android.greekgods; import android.content.Context; +import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.ConnectivityManager; @@ -24,6 +25,7 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import android.view.View; import android.widget.TextView; import java.io.BufferedReader; @@ -47,6 +49,10 @@ protected void onCreate(Bundle savedInstanceState) { fillDatabase(); } + public void onClickAllPeople(View view) { + startActivity(new Intent(this, AllPeopleActivity.class)); + } + private void fillDatabase() { new AsyncTask() { @Override @@ -74,17 +80,23 @@ protected Void doInBackground(Void... params) { String peopleSQLStatements = downloadFile("http://teinvdlugt.netai.net/people.sql"); if (peopleSQLStatements != null) { peopleSQLStatements = peopleSQLStatements.replaceAll("kcv.people", "people"); - db.execSQL(peopleSQLStatements); + String[] statements = peopleSQLStatements.split("\n"); + for (String statement : statements) + db.execSQL(statement); } String relationsSQLStatements = downloadFile("http://teinvdlugt.netai.net/relations.sql"); if (relationsSQLStatements != null) { relationsSQLStatements = relationsSQLStatements.replaceAll("kcv.relations", "relations"); - db.execSQL(relationsSQLStatements); + String[] statements = relationsSQLStatements.split("\n"); + for (String statement : statements) + db.execSQL(statement); } String birthsSQLStatements = downloadFile("http://teinvdlugt.netai.net/births.sql"); if (birthsSQLStatements != null) { birthsSQLStatements = birthsSQLStatements.replaceAll("kcv.births", "births"); - db.execSQL(birthsSQLStatements); + String[] statements = birthsSQLStatements.split("\n"); + for (String statement : statements) + db.execSQL(statement); } db.close(); diff --git a/app/src/main/java/com/teinvdlugt/android/greekgods/models/Person.java b/app/src/main/java/com/teinvdlugt/android/greekgods/models/Person.java index 3ecfd13..7c684f2 100644 --- a/app/src/main/java/com/teinvdlugt/android/greekgods/models/Person.java +++ b/app/src/main/java/com/teinvdlugt/android/greekgods/models/Person.java @@ -22,6 +22,7 @@ public class Person { private String name; private String description, shortDescription; private List relations; + private int id; public Person(String name, List relations) { this.name = name; @@ -31,6 +32,14 @@ public Person(String name, List relations) { public Person() { } + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + public String getName() { return name; } diff --git a/app/src/main/res/layout/activity_all_people.xml b/app/src/main/res/layout/activity_all_people.xml new file mode 100644 index 0000000..d19220b --- /dev/null +++ b/app/src/main/res/layout/activity_all_people.xml @@ -0,0 +1,27 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 85493fa..2f62ffd 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,9 +1,10 @@ - - + +