0

I am developing an app that uses a RecyclerView, so I made a bunch of dummy objects to add to the RecyclerView so I can see what it looks like when it's full.

The problem is, I removed the code, uninstalled the app, then reinstalled it (to clear the database), but the dummy objects keep showing up. If I delete them then run the app again from my computer, the objects all come back.

The dummy objects are also only supposed to run once, when the app is installed, so the run button shouldn't add the objects anyways.

Here's my OpenDbHelper, where the objects are added:

OpenDbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    starterContext = context;
}

public void onCreate(SQLiteDatabase db) {
    db.execSQL(GoalsContract.Goals.SQL_CREATE_GOALS);
    db.execSQL(CategoriesContract.Categories.SQL_CREATE_CATEGORIES);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(starterContext);
    if(!prefs.getBoolean("firstTime", false)) {
        addDefaultData(db);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("firstTime", true);
        editor.apply();
    }
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(GoalsContract.Goals.SQL_DELETE_GOALS);
    db.execSQL(CategoriesContract.Categories.SQL_DELETE_CATEGORIES);
    onCreate(db);
}

private void addDefaultData(SQLiteDatabase db) {
    String[] defaultCategories = {"Work", "Fitness", "Family", "Finance"};

    for(String currentName : defaultCategories) {
        ContentValues values = new ContentValues();
        values.put(CategoriesContract.Categories.COLUMN_NAME_NAME, currentName);
        db.insert(CategoriesContract.Categories.TABLE_NAME, null, values);
    }

    /*
    Goal loseWeight = new Goal();
    loseWeight.setGoalName("Lose weight");
    loseWeight.setDueDate(Utility.formatStringToGregorian("05/19/2017 00:00"));
    loseWeight.setCategory("Fitness");
    loseWeight.setNotes("");

    Goal doTaxes = new Goal();
    doTaxes.setGoalName("Do my taxes");
    doTaxes.setDueDate(Utility.formatStringToGregorian("04/01/2018 00:00"));
    doTaxes.setCategory("Work");
    doTaxes.setNotes("Hire accountant.");

    ContentValues values = Utility.goalObjectToContentValues(loseWeight);
    db.insert(GoalsContract.Goals.TABLE_NAME, null, values);

    values = Utility.goalObjectToContentValues(doTaxes);
    db.insert(GoalsContract.Goals.TABLE_NAME, null, values);

    for (int x = 0; x < 15; x++) {
        ContentValues values = new ContentValues();
        values.put(GoalsContract.Goals.COLUMN_NAME_GOAL_NAME, "I'm dummy #" + (x+1));
        values.put(GoalsContract.Goals.COLUMN_NAME_DATE_AND_TIME, "06/19/2017 00:00");
        values.put(GoalsContract.Goals.COLUMN_NAME_CATEGORY, "Work");

        db.insert(GoalsContract.Goals.TABLE_NAME, null, values);
    }
    */
}
AJB
  • 67
  • 6

1 Answers1

0

The firstTime variable from the SharedPreferences was also defaulting to true, even though it should always be false, unless the code was run. Looking into that problem, it seems as though the android:allowBackup property in the manifest was set to true.

Got my solution from this answer.

AJB
  • 67
  • 6