SharedPreferences and AlertDialog
I am a beginner in Android app development who is doing a single
dictionary app based on an open source project. The open source project is
capable to install many dictionaries in single app. However, what I want
is 1 dictionary in 1 app. So I need to modify the project from capable to
install many dictionaries to just able to install 1 dictionary. In order
to accomplish this, I have identified the following code from the source
code:
dictName = sharedPreferences.getString(PREFERENCES_DICT_NAME, null);
if (dictName == null) {
showChooseDictDialog(false);
} else {
So when the app is running, it will retrieve the sharedPreferences called
dictName. If there is no dictionary, an AlertDialog will be shown:
private void showChooseDictDialog(boolean cancelable) {
AlertDialog.Builder dictChooseBuilder = new AlertDialog.Builder(this);
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return filename.contains(".ifo");
}
};
dictFilenames = dictPathFile.list(filter);
if (dictFilenames.length > 0) {
dictChooseBuilder.setItems(dictFilenames, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dictName = dictFilenames[which].substring(0,
dictFilenames[which].indexOf(".ifo"));
modifyDictVar(dictName);
initDbFile();
databaseHelper.initDb();
if (new File(ifoFileName).isFile()) {
setTitle(loadDictInfo());
}
if (new File(idxFileName).isFile() && dictFile.isFile()) {
sharedPreferences.edit().putString(ZhuangDictActivity.PREFERENCES_DICT_NAME,
dictName).commit();
The AlertDialog will ask user to select a dictionary and eventually write
the sharedPreferences.
So my question is, if the dictionary which I intended to use is just
dictionary.ifo, it is a redundant step to ask users to select a dictionary
through AlertDialog and monitor it by OnClickListener. How should I modify
the code so that dictionary.ifo will be autoclick or is there any other
better way?
No comments:
Post a Comment