Skip to content

Commit

Permalink
Create AllPeopleActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
teinvdlugt committed Feb 20, 2016
1 parent 82faf9f commit 24252ea
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 8 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".AllPeopleActivity"
android:label="@string/all_people" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -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<Person>(), this);
recyclerView.setAdapter(adapter);
refresh();
}

private void refresh() {
new AsyncTask<Void, Void, List<Person>>() {
@Override
protected List<Person> 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<Person> 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<Person> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<AllPeopleRecyclerViewAdapter.ViewHolder> {

private Context context;
private List<Person> data;
private OnPersonClickListener onPersonClickListener;

public AllPeopleRecyclerViewAdapter(Context context, List<Person> data, OnPersonClickListener onPersonClickListener) {
this.context = context;
this.data = data;
this.onPersonClickListener = onPersonClickListener;
}

public interface OnPersonClickListener {
void onClickPerson(Person person);
}

public void setData(List<Person> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
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;
import android.net.NetworkInfo;
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;
Expand All @@ -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<Void, Void, Void>() {
@Override
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Person {
private String name;
private String description, shortDescription;
private List<Relation> relations;
private int id;

public Person(String name, List<Relation> relations) {
this.name = name;
Expand All @@ -31,6 +32,14 @@ public Person(String name, List<Relation> relations) {
public Person() {
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/res/layout/activity_all_people.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
app:popupTheme="@style/AppTheme.PopupOverlay"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|snap|enterAlways" />
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
11 changes: 9 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
Expand All @@ -15,4 +16,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/all_people"
android:onClick="onClickAllPeople"/>
</LinearLayout>
30 changes: 30 additions & 0 deletions app/src/main/res/layout/list_item_all_people.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?selectableItemBackground"
android:orientation="vertical">

<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@android:color/darker_gray" />

<TextView
android:id="@+id/name_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:textColor="@android:color/black"
android:textSize="18sp"
tools:text="Chaos" />

<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@android:color/darker_gray" />
</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">Griekse Goden</string>
<string name="all_people">Alle familieleden</string>
</resources>
7 changes: 4 additions & 3 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

0 comments on commit 24252ea

Please sign in to comment.