-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate database in FamilyTreeActivity
- Loading branch information
1 parent
e47322c
commit 92f9f3f
Showing
4 changed files
with
99 additions
and
34 deletions.
There are no files selected for viewing
85 changes: 72 additions & 13 deletions
85
app/src/main/java/com/teinvdlugt/android/greekgods/FamilyTreeActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,89 @@ | ||
package com.teinvdlugt.android.greekgods; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.database.Cursor; | ||
import android.database.CursorIndexOutOfBoundsException; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.os.AsyncTask; | ||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v7.app.AppCompatActivity; | ||
|
||
import com.teinvdlugt.android.greekgods.models.Person; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FamilyTreeActivity extends AppCompatActivity { | ||
|
||
FamilyTreeLayout treeLayout; | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_family_tree); | ||
|
||
FamilyTreeLayout treeLayout = (FamilyTreeLayout) findViewById(R.id.family_tree_layout); | ||
Person person = new Person(); | ||
person.setName("Zeus"); | ||
person.setShortDescription("Oppergod van Olympus"); | ||
Person parent1 = new Person(); | ||
parent1.setName("Rhea"); | ||
parent1.setShortDescription("Titaan"); | ||
Person parent2 = new Person(); | ||
parent2.setName("Kronos"); | ||
parent2.setShortDescription("Titaan"); | ||
person.setParent1(parent1); | ||
person.setParent2(parent2); | ||
treeLayout.setPerson(person); | ||
treeLayout = (FamilyTreeLayout) findViewById(R.id.family_tree_layout); | ||
loadPerson(64); | ||
} | ||
|
||
@SuppressLint("DefaultLocale") | ||
private void loadPerson(final int personId) { | ||
new AsyncTask<Void, Void, Person>() { | ||
@Override | ||
protected Person doInBackground(Void... params) { | ||
Person person = getBasicPersonData(personId); | ||
List<Integer> parentIds = getParentIds(personId); | ||
List<Person> parents = new ArrayList<>(); | ||
for (int parentId : parentIds) { | ||
parents.add(getBasicPersonData(parentId)); | ||
} | ||
person.setParents(parents); | ||
return person; | ||
} | ||
|
||
@Deprecated | ||
private List<Integer> getParentIds(int personId) { | ||
List<Integer> result = new ArrayList<>(); | ||
Cursor c = null; | ||
|
||
try { | ||
SQLiteDatabase db = openOrCreateDatabase("data", 0, null); | ||
String query = String.format(DBUtils.PARENTS_QUERY, personId); | ||
c = db.rawQuery(query, null); | ||
c.moveToFirst(); | ||
do { | ||
result.add(c.getInt(c.getColumnIndex("personId"))); | ||
} while (c.moveToNext()); | ||
} catch (CursorIndexOutOfBoundsException ignored) { | ||
} finally { if (c != null) c.close(); } | ||
|
||
return result; | ||
} | ||
|
||
private Person getBasicPersonData(int id) { | ||
Person person = new Person(id); | ||
person.setId(id); | ||
Cursor c = null; | ||
try { | ||
SQLiteDatabase db = openOrCreateDatabase("data", 0, null); | ||
String[] columns = {"name", "shortDescription"}; | ||
String[] selectionArgs = {String.valueOf(id)}; | ||
c = db.query("people", columns, "personId=?", selectionArgs, null, null, null); | ||
c.moveToFirst(); | ||
person.setName(c.getString(c.getColumnIndex("name"))); | ||
person.setShortDescription(c.getString(c.getColumnIndex("shortDescription"))); | ||
} finally { | ||
if (c != null) c.close(); | ||
} | ||
|
||
return person; | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(Person person) { | ||
treeLayout.setPerson(person); | ||
} | ||
}.execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
<HorizontalScrollView | ||
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" | ||
tools:context=".FamilyTreeActivity"> | ||
|
||
<com.teinvdlugt.android.greekgods.FamilyTreeLayout | ||
android:id="@+id/family_tree_layout" | ||
android:layout_width="match_parent" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center_vertical" /> | ||
</FrameLayout> | ||
android:layout_gravity="center_vertical|center_horizontal" /> | ||
</HorizontalScrollView> |