Commit e3bba0fc authored by Adrien Oliva's avatar Adrien Oliva

feat(character) Map character in database

Characters stored in database are now shown on character switch
activity.

GTLAB #4Signed-off-by: Adrien Oliva's avatarAdrien Oliva <olivaa+gitlab@yapbreak.fr>
parent df4aacf0
Pipeline #482 passed with stage
in 0 seconds
......@@ -28,13 +28,14 @@
android:label="@string/title_activity_character_creation"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".CharacterSwitch"
android:label="@string/title_activity_character_switch"
android:theme="@style/AppTheme.NoActionBar" />
<meta-data
android:name="com.google.android.actions"
android:resource="@xml/backup_rules" />
<activity android:name=".CharacterSwitch"
android:label="@string/title_activity_character_switch"
android:theme="@style/AppTheme.NoActionBar" />
</application>
</manifest>
\ No newline at end of file
......@@ -2,12 +2,28 @@ package fr.yapbreak.skyrimquest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;
import fr.yapbreak.skyrimquest.character.CharacterCursorAdapter;
import fr.yapbreak.skyrimquest.data.DataHelper;
public class CharacterSwitch extends AppCompatActivity {
DataHelper data;
ListView mainListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_switch);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mainListView = findViewById(R.id.character_listview);
data = new DataHelper(this);
CharacterCursorAdapter adapter = new CharacterCursorAdapter(this, data.getAllCharacters());
mainListView.setAdapter(adapter);
}
}
package fr.yapbreak.skyrimquest.character;
import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.lang.reflect.Field;
import fr.yapbreak.skyrimquest.R;
public class CharacterCursorAdapter extends CursorAdapter {
private static class ViewHolder {
ImageView avatar;
TextView name;
TextView race;
TextView level;
}
public CharacterCursorAdapter(Context context, Cursor c) {
super(context, c, true);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
LayoutInflater inflater = LayoutInflater.from(context);
View myView = inflater.inflate(R.layout.character_fragment, viewGroup, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.avatar = myView.findViewById(R.id.character_avatar);
viewHolder.name = myView.findViewById(R.id.character_name_label);
viewHolder.race = myView.findViewById(R.id.character_race_label);
viewHolder.level = myView.findViewById(R.id.character_level_label);
myView.setTag(viewHolder);
return myView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
String character_name = cursor.getString(1);
String character_avatar_path = cursor.getString(2);
String character_race_res_id = cursor.getString(3);
Integer character_level = cursor.getInt(4);
String race_label;
try {
Field translatedRace = R.string.class.getDeclaredField(character_race_res_id);
race_label = context.getString(translatedRace.getInt(translatedRace));
} catch (Exception e) {
race_label = character_race_res_id;
}
String character_level_label = context.getString(R.string.character_level_default) + " " + character_level.toString();
viewHolder.name.setText(character_name);
viewHolder.level.setText(character_level_label);
viewHolder.race.setText(race_label);
viewHolder.avatar.setImageBitmap(BitmapFactory.decodeFile(character_avatar_path));
}
}
......@@ -288,6 +288,13 @@ public class DataHelper extends SQLiteOpenHelper {
return db.rawQuery("SELECT " + PK_RACE_ID + " AS _id, " + RESOURCE_ID_STRING + " FROM " + RACE_TABLE + " ORDER BY _id", null);
}
public Cursor getAllCharacters()
{
SQLiteDatabase db = getReadableDatabase();
Log.d("SKYRIM", "SELECT " + PK_CHARACTER_ID + " AS _id, " + NAME + ", " + AVATAR_FILENAME + ", R." + RESOURCE_ID_STRING + ", " + LEVEL + " FROM " + CHARACTER_TABLE + ", " + RACE_TABLE + " AS R");
return db.rawQuery("SELECT " + PK_CHARACTER_ID + " AS _id, " + NAME + ", " + AVATAR_FILENAME + ", R." + RESOURCE_ID_STRING + ", " + LEVEL + " FROM " + CHARACTER_TABLE + ", " + RACE_TABLE + " AS R WHERE " + FK_RACE_ID + " = R." + PK_RACE_ID, null);
}
public int getCharacterId(Character c)
{
SQLiteDatabase db = getReadableDatabase();
......
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:id="@+id/activity_character_switch"
tools:context=".CharacterSwitch"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_character_switch" >
<ListView
android:id="@+id/character_listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_height="match_parent">
</ListView>
</android.support.constraint.ConstraintLayout>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment