英文:
On Upgrade method is called but data not loading
问题
以下是代码部分的翻译内容:
// 在ListViewActivity的onCreate方法中
// 初始化用于存储布尔值的SharedPreferences键
INITIALIZED = "initialized_database_" + class_name;
// 初始化SharedPreferences以避免空指针异常
sharedPreferences = getSharedPreferences(INITIALIZED, MODE_PRIVATE);
myDictionaryDatabaseHelper = new DictionaryDatabaseHelper(this, db_name);
allWordDefinitions = new ArrayList<>();
initialized = sharedPreferences.getBoolean(INITIALIZED, false);
// 如果未初始化数据库,则进行初始化
if (!initialized) {
// 初始化数据库
initializeDatabase();
}
else {
//Log.e(logTagString, myDictionaryDatabaseHelper.getDatabaseName() + " 已经初始化。");
}
// initializeDatabase方法
// 初始化数据库
private void initializeDatabase() {
editor = sharedPreferences.edit();
try {
InputStream inputStream = getResources().openRawResource(db_resource);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
DictionaryLoader.loadData(bufferedReader, myDictionaryDatabaseHelper);
Toast.makeText(this, "正在初始化数据库...", Toast.LENGTH_SHORT).show();
// 更改SharedPreferences编辑器中的布尔值
editor.putBoolean(INITIALIZED, true);
editor.apply();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
// 弹出消息提示
Toast.makeText(this, "找不到数据库资源!", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "数据库初始化失败!", Toast.LENGTH_SHORT).show();
// 将初始化布尔值设置为false,以便重新初始化数据库
editor.putBoolean(INITIALIZED, false);
editor.apply();
}
}
// DictionaryDatabaseHelper类
class DictionaryDatabaseHelper extends SQLiteOpenHelper {
private static String DB_NAME = "db_name";
private final static int DB_VERSION = 2;
private final static String ID_COLUMN = "_id";
private final static String WORD_COLUMN = "word";
private final static String DEFINITION_COLUMN = "definition";
private final String CREATE_DATABASE_QUERY = "CREATE TABLE "
+ getDatabaseName() + " ("
+ ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ WORD_COLUMN + " TEXT, "
+ DEFINITION_COLUMN + " TEXT)";
private final String UPGRADE_QUERY = "DROP TABLE IF EXISTS " + getDatabaseName();
// 构造函数用于创建具有不同名称的数据库
DictionaryDatabaseHelper(Context context, String db_name) {
super(context, db_name, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DATABASE_QUERY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// 将数据库初始化变量设置为false,以便重新初始化数据库
WordListActivity.editor.putBoolean(WordListActivity.INITIALIZED, false);
WordListActivity.editor.apply();
db.execSQL(UPGRADE_QUERY);
onCreate(db);
}
// 第一次初始化数据库
void initializeDatabaseForTheFirstTime(ArrayList<WordDefinition> wordDefinitions) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("BEGIN");
ContentValues contentValues = new ContentValues();
for (WordDefinition wordDefinition : wordDefinitions) {
contentValues.put(WORD_COLUMN, wordDefinition.word);
contentValues.put(DEFINITION_COLUMN, wordDefinition.definition);
db.insert(getDatabaseName(), null, contentValues);
}
db.execSQL("COMMIT");
db.close();
}
}
以上是您提供的代码部分的翻译内容。如果您有任何问题或需要进一步帮助,请随时提问。
英文:
I have my first activity (MainActivity
) having different letters (like A, B, C...). When click on a letter it opens a new activity (LIstViewActivity
) having a listView
of words starting with that letter. This list is created from a text file inside raw
folder and stored in a database
named suppose database_a
. In LIstViewActivity
I am storing a boolean
in SharedPreferences
which checks whether database
of that particular letter is created or not. If it is not created it runs the initializeDatabase
method otherwise not. These are working fine. But if I increase the database version
then onUpgrade
method is called but the list
is not populating. I also tried to change the boolean
value inside onUpgrade
method. In this case on my first attempt data is not populating, when I go back and come again then the data are loading. I think the problem might be due to the boolean
value. How can I overcome the problem? Any help will be appreciated. Thanks...
This is inside onCreate
of ListViewActivity
//initialize key for the boolean in sharedPreference
INITIALIZED = "initialized_database_"+class_name;
//initialize sharedPreference to avoid NullPointerException
sharedPreferences = getSharedPreferences(INITIALIZED, MODE_PRIVATE);
myDictionaryDatabaseHelper = new DictionaryDatabaseHelper(this, db_name);
allWordDefinitions = new ArrayList<>();
initialized = sharedPreferences.getBoolean(INITIALIZED, false);
//initialize database
if (!initialized) {
//initialize database
initializeDatabase();
}
else {
//Log.e(logTagString, myDictionaryDatabaseHelper.getDatabaseName() +" already initialized.");
}
This is initializeDatabase
method
//initialize the database
private void initializeDatabase() {
editor = sharedPreferences.edit();
try {
InputStream inputStream = getResources().openRawResource(db_resource);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
DictionaryLoader.loadData(bufferedReader, myDictionaryDatabaseHelper);
Toast.makeText(this, "Initializing database...", Toast.LENGTH_SHORT).show();
//Log.e(logTagString, "Initializing " + myDictionaryDatabaseHelper.getDatabaseName());
//change boolean value in sharedPreference editor
editor.putBoolean(INITIALIZED, true);
editor.apply();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
//Log.e(logTagString, myDictionaryDatabaseHelper.getDatabaseName() +" resource not found!");
//Log.e(logTagString, myDictionaryDatabaseHelper.getDatabaseName() +" initialization failed!");
//toast a message
Toast.makeText(this, "Database resource not found!", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Database initialization failed!", Toast.LENGTH_SHORT).show();
//keep the initialized boolean false to reinitialize the database
editor.putBoolean(INITIALIZED, false);
editor.apply();
}
}
This is my DictionaryDatabaseHelper
class
class DictionaryDatabaseHelper extends SQLiteOpenHelper {
private static String DB_NAME = "db_name";
private final static int DB_VERSION = 2;
private final static String ID_COLUMN = "_id";
private final static String WORD_COLUMN = "word";
private final static String DEFINITION_COLUMN = "definition";
private final String CREATE_DATABASE_QUERY = "CREATE TABLE "
+getDatabaseName()+ " ("
+ID_COLUMN+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+WORD_COLUMN+ " TEXT, "
+DEFINITION_COLUMN+ " TEXT)";
private final String UPGRADE_QUERY = "DROP TABLE IF EXISTS " +getDatabaseName();
//constructor to create different named database
DictionaryDatabaseHelper(Context context, String db_name) {
super(context, db_name, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DATABASE_QUERY);
//Log.e("DICTIONARY", "DatabaseHelper onCreate Initializing database...");
//Log.e("DICTIONARY", "Database name is: " +getDatabaseName());
//Log.e("DICTIONARY", "Database version is "+db.getVersion());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//make the database initialization variable false to reinitialize the database
WordListActivity.editor.putBoolean(WordListActivity.INITIALIZED, false);
WordListActivity.editor.apply();
//Log.e("DICTIONARY", "Initialized value is: " +WordListActivity.initialized);
db.execSQL(UPGRADE_QUERY);
onCreate(db);
//Log.e("DICTIONARY", "Upgrading database form version " +oldVersion+ " to " +newVersion);
}
//Initialize database for the first time
void initializeDatabaseForTheFirstTime(ArrayList<WordDefinition> wordDefinitions) {
//Log.e("DICTIONARY", "Initializing database for the first time.");
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("BEGIN");
ContentValues contentValues = new ContentValues();
for (WordDefinition wordDefinition : wordDefinitions) {
contentValues.put(WORD_COLUMN, wordDefinition.word);
contentValues.put(DEFINITION_COLUMN, wordDefinition.definition);
db.insert(getDatabaseName(), null, contentValues);
}
db.execSQL("COMMIT");
db.close();
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论