私はObjectBoxライブラリを使ってAndroidでデータベースを管理しています。データベースにあらかじめ定義された値を設定する方法
アプリケーションを初めてインストールまたは起動したときにのみ初期化されるデータベースに定義済みの値を設定したいとします。
今のところ、私は定数の束を定義する必要があります。アプリケーション内ではデータベースから取得する必要があります。たとえば、それは行の_id
を提示するか、またはコードにimage
を提供する必要があります。
私は今、このようにそれをやっている:
public class Constants {
public static final int CATEGORY_FOOD = 1;
public static final int CATEGORY_SHOPPING = 2;
public static final int CATEGORY_HOUSING = 3;
public static final int CATEGORY_TRANSPORTATION = 4;
public static final int CATEGORY_VEHICLE = 5;
public static final int CATEGORY_LIFE = 6;
public static final int CATEGORY_COMMUNICATION = 7;
public static final int CATEGORY_FINANCIAL_EXPENSES = 8;
public static final int CATEGORY_BAD_HABBITS = 9;
public static final int CATEGORY_EDUCATION = 10;
public static final int CATEGORY_INVESTMENTS = 11;
public static final int CATEGORY_INCOME = 12;
public static final int CATEGORY_ICON_FOOD = R.drawable.ic_food;
public static final int CATEGORY_ICON_SHOPPING = R.drawable.ic_shopping;
public static final int CATEGORY_ICON_HOUSING = R.drawable.ic_housing;
public static final int CATEGORY_ICON_TRANSPORTATION = R.drawable.ic_transportation;
public static final int CATEGORY_ICON_VEHICLE = R.drawable.ic_car;
public static final int CATEGORY_ICON_LIFE = R.drawable.ic_tickets;
public static final int CATEGORY_ICON_COMMUNICATION = R.drawable.ic_communication;
public static final int CATEGORY_ICON_FINANCIAL_EXPENSES = R.drawable.ic_voucher;
public static final String TYPE_EXPENSE = "expense";
public static final String TYPE_INCOME = "income";
}
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
defineCategories();
}
private void defineCategories() {
/* Shared Preferences value */
if (defineVersion != 1) return;
Category category1 = new Category();
category1.setTitle("Food & Drinks");
category1.setType(TYPE_EXPENSE);
mCategoryBox.put(category1);
Category category2 = new Category();
category2.setTitle("Shopping");
category2.setType(TYPE_EXPENSE);
mCategoryBox.put(category2);
Category category3 = new Category();
category3.setTitle("Housing");
category3.setType(TYPE_EXPENSE);
mCategoryBox.put(category3);
Category category24 = new Category();
category24.setTitle("Toll road");
category24.setType(TYPE_EXPENSE);
Category category4 = new Category();
category4.setTitle("Transportation");
category4.setType(TYPE_EXPENSE);
category4.subCategories.add(category24);
mCategoryBox.put(category4);
Category category5 = new Category();
category5.setTitle("Vehicle");
category5.setType(TYPE_EXPENSE);
mCategoryBox.put(category5);
}
}
は、この必要性のためのより多くの、より良い解決策はありますか?
データベースクラスはどこですか?データベースクラスを提供します。 –
データベースクラスはありません、私はObjectBoxライブラリを使用しています。 – aleksandrbel