I'm currently setting up a dungeons & Dragons mobile application in android studio and I want to be able to view the values for the character that I am currently saving into a Database "CharacterDB". How can I implement that in such a way that will let me find the values to pull them for later use? Any help or advice would go a long way, thank you.

I've Tried already using the query method to go through a selection of the items in current row to no avail. Whenever I tried to set up the correct query with a cursor it would say that that the cursor would be NULL whenever I attempted to loop through it.

///////////////////// What I currently have /////////////////////

CharacterBaseHelper helper = new CharacterBaseHelper(getContext()); 

SQLiteDatabase Character = helper.getReadableDatabase();

ContentValues values = new ContentValues();

//Inserting test values into the Database

values.put(CharacterDB.CharacterTable.CharactersColumns.NAME, 
"Character");

values.put(CharacterDB.CharacterTable.CharactersColumns.CLASS_NAME, 
"Fighter"  );

values.put(CharacterDB.CharacterTable.CharactersColumns.RACE, "Human"  );

//saving the row ID for late use

Long newCharacterRowId = 
Character.insert(CharacterDB.CharacterTable.TABLE_NAME, null , values);

///////////////////////////////////////////////

Then this the table I am creating for the values and the helper class

//////////////////////////////////////////////

 public final class CharacterTable
{
    public static final String TABLE_NAME = "character";

    public  final class CharactersColumns implements BaseColumns
    {
        public static final String NAME = "name";
        public static final String CLASS_NAME = "className";
        public static final String RACE = "race";


}

///////////////////

The Helper Class

///////////////////

public class CharacterBaseHelper extends SQLiteOpenHelper {

private static final int VERSION = 1;

private static final String DATABASE_NAME = "characterDB.db";


public CharacterBaseHelper(Context context){

 super(context, DATABASE_NAME, null, VERSION);

 }

@Override

public void onCreate(SQLiteDatabase db)
{

db.execSQL(
 "create table " + CharacterTable.TABLE_NAME + "(" + " _id integer primary 
key autoincrement, "
            + CharacterTable.CharactersColumns.NAME
            + "," + CharacterTable.CharactersColumns.CLASS_NAME
            + "," + CharacterTable.CharactersColumns.RACE + ")");

/////////////////////

Lastly, what I tried before

////////////////////

String[] projection = {
                                BaseColumns._ID,

CharacterDB.CharacterTable.CharactersColumns.NAME,

CharacterDB.CharacterTable.CharactersColumns.HIT_DICE,
                        };

                    String selection = CharacterDB.CharacterTable.CharactersColumns.NAME + " = ?";
                    String[] selectionArgs = { "Character" };

                    String sortOrder =
                            CharacterDB.CharacterTable.CharactersColumns.HIT_DICE + " DESC";

                    Cursor cursor = Character.query(
                            CharacterDB.CharacterTable.TABLE_NAME,   // The table to query
                            projection,             // The array of columns to return (pass null to get all)
                            selection,              // The columns for the WHERE clause
                            selectionArgs,          // The values for the WHERE clause
                            null,                   // don't group the rows
                            null,                   // don't filter by row groups
                            sortOrder               // The sort order
                    );
                    List item = new ArrayList<>();
                    while(cursor.moveToNext()) {
                        long itemId = cursor.getLong(
                                cursor.getColumnIndexOrThrow(CharacterDB.CharacterTable.CharactersColumns._ID));
                        item.add(itemId);
                    }
                    cursor.close();

What I've been trying to obtain is successful way to read the new data from the database and display it so that the user can see the end result of their character.

1 Answers

3
Halcn On

This shows a way to get the values so you could view them :-

    // all rows are gotten
    Cursor AllCharacter = Character.query(CharacterTable.TABLE_NAME,null,null,null,null,null,null);
    while (AllCharacter.moveToNext()) {
        Log.v("ALLCHARACTER", BaseColumns._ID + " is " + AllCharacter.getLong(AllCharacter.getColumnIndex(BaseColumns._ID))
                + " " + CharacterTable.CharactersColumns.NAME + " is " + AllCharacter.getString(AllCharacter.getColumnIndex(CharacterTable.CharactersColumns.NAME))
                + " " + CharacterTable.CharactersColumns.CLASS_NAME + " is " + AllCharacter.getString(AllCharacter.getColumnIndex(CharacterTable.CharactersColumns.CLASS_NAME))
                + " " + CharacterTable.CharactersColumns.RACE + " is " + AllCharacter.getString(AllCharacter.getColumnIndex(CharacterTable.CharactersColumns.RACE))
        );
    }

log will have a line like

ALLCHARACTER: _id is 1 name is Character className is Fighter race is Human

Wanting to look for a specific example the you could use :-

    // getting a specific row examle using the _id column
    Cursor OneCharacter = Character.query(CharacterTable.TABLE_NAME,

            // Pramater 2 columns specific null all columns
            new String[]{
                    CharacterTable.CharactersColumns.NAME,
                    CharacterTable.CharactersColumns.CLASS_NAME,
                    CharacterTable.CharactersColumns.RACE,
                    BaseColumns._ID
            },
            BaseColumns._ID + "=?", // WHERE bit no WHERE writting use ? for no SQL injectioning with 4th paramter
            new String[]{"1"}, // 1th ? changes 1st ? 3rd parameter and soing on
            null,null,null
    );

You could use a ListView to view lots of rows using a SimpleCursorAdapter. For example

    //Add another row the ListView
    values.clear();
    values.put(CharacterTable.CharactersColumns.NAME,"CharacaterMore");
    values.put(CharacterTable.CharactersColumns.CLASS_NAME,"Defencer");
    values.put(CharacterTable.CharactersColumns.RACE,"Maceman");
    Character.insert(CharacterTable.TABLE_NAME,null,values);

    // all rows
    // Using final is not good but for ease has been used
    final Cursor AllCharacter = Character.query(CharacterTable.TABLE_NAME,null,null,null,null,null,null);
    while (AllCharacter.moveToNext()) {
        Log.v("ALLCHARACTER", BaseColumns._ID + " is " + AllCharacter.getLong(AllCharacter.getColumnIndex(BaseColumns._ID))
                + " " + CharacterTable.CharactersColumns.NAME + " is " + AllCharacter.getString(AllCharacter.getColumnIndex(CharacterTable.CharactersColumns.NAME))
                + " " + CharacterTable.CharactersColumns.CLASS_NAME + " is " + AllCharacter.getString(AllCharacter.getColumnIndex(CharacterTable.CharactersColumns.CLASS_NAME))
                + " " + CharacterTable.CharactersColumns.RACE + " is " + AllCharacter.getString(AllCharacter.getColumnIndex(CharacterTable.CharactersColumns.RACE))
        );
    }


    // ListView example
    ListView AllCharacterListView = (ListView) findViewById(R.id.allcharacter);
    SimpleCursorAdapter AllCharacterListAdapter = new SimpleCursorAdapter(this, // getContext if fragment
            android.R.layout.simple_list_item_2,
            AllCharacter,
            new String[]{CharacterTable.CharactersColumns.NAME, CharacterTable.CharactersColumns.RACE},
            new int[]{android.R.id.text1,android.R.id.text2},
            0
    );
    AllCharacterListView.setAdapter(AllCharacterListAdapter);
    // Adding onItemClick listener to show a Toast for the clicked item
    AllCharacterListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(view.getContext(),
                    "Item clicking sees " + String.valueOf(id)
                            + AllCharacter.getString(AllCharacter.getColumnIndex(CharacterTable.CharactersColumns.NAME)) ,
                    Toast.LENGTH_LONG).show();
        }
    });
}

This example results in :-

enter image description here