1

I'm working on an Achievement system for Android and I'm just looking for a simple way to store all the achievements and your progress for each. I need to store: id, title, description, points, current value, max value, and an image path for EACH achievement.

I tried using the SQLite services and everything but I can't seem to get that working that well, so I just want to use some basic text tables for now. I know you use the FileBuffer and I know how to write to text files, I'm just not sure the best approach to format them and how to read them in.

I was thinking of formatting the table something like this:

{
    {
        id = 0,
        Title = "Sample Achievement",
        Desc = "Sample description",
        Points = 5,
        CurVal = 3,
        MaxVal = 10,
        Img = "path/to/img.jpg",
     }
     {
        id = 1;
        ...

That would give me access to each achievement as its own subtable, or something like that and I can build an Achievement object around the values I get back for later use.

1 Answer 1

0

you can start creating a database like below.

private static String db_path = "/data/data/com.yourproject/databases/mysqlitedb";
private SQLiteDatabase sqliteDatabase = null;

public SQLiteDatabase getSQLiteDatabase() {
    try {

        sqliteDatabase = SQLiteDatabase.openDatabase(db_path, null,
                SQLiteDatabase.OPEN_READWRITE);
        sqliteDatabase.setVersion(1);
        sqliteDatabase.setLocale(Locale.getDefault());
        sqliteDatabase.setLockingEnabled(true);

        return sqliteDatabase;
    } catch (Exception e) {
        e.getMessage();
        return null;
    }

}

after that you can create your table like below.

public void createTable() {
    try {

        if (sqliteDatabase != null) {
            sqliteDatabase.execSQL("CREATE TABLE IF NOT EXISTS "
                    + "Achievement" + " (_id INTEGER PRIMARY KEY, Title TEXT, Desc TEXT,"
                    + " Points NUMERIC," + " CurVal NUMERIC, MaxVal NUMERIC, "
                    + " Img TEXT);");


        }
    } catch (Exception e) {
        e.getMessage();
    } 

}

you can insert data to your table like below.

public int insert(String table, Hashtable<String, String> FieldsAndValues) {
    // TODO Auto-generated method stub
    int count=1;
    try {
        if (sqliteDatabase == null) {
            sqliteDatabase = getSQLiteDatabase();
        }

        Enumeration e = FieldsAndValues.elements();
        ContentValues initialValues = new ContentValues();
        for (e = FieldsAndValues.keys(); e.hasMoreElements();) {
            String key = (String) e.nextElement();
            String value = (String) FieldsAndValues.get(key);

            initialValues.put(key, value);

        }

        sqliteDatabase.insert(table, null, initialValues);

        return count;

    } catch (Exception e) {
        e.getMessage();
        return -1;
    }
}

Remember that, all these three method should be under one class.

if you able to manage up-to creating database and your table(by calling these methods in your initial activity) inserting data will be a piece of cake, will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.