0
我用下面的代碼保存字的歷史在我的字典應用程序:SharedPreferences:歷史已保存,但爲什麼不收藏?
@Override
public void onPause()
{
super.onPause();
saveHistoryToPreferences();
}
public void saveHistoryToPreferences()
{
if (prefs.getBoolean("saveHistory", true) && mWordHistory != null && mWordHistory.size() >= 1)
{
StringBuilder sbHistory = new StringBuilder();
for (String item : mWordHistory)
{
sbHistory.append(item);
sbHistory.append(",");
}
String strHistory = sbHistory.substring(0, sbHistory.length()-1);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("history", strHistory);
editor.commit();
//Log.i(CONTENT_TAG,"history = " + strHistory);
Log.i(CONTENT_TAG,"History saved!");
}
}
public void loadHistoryFromPreferences()
{
if (prefs.getBoolean("saveHistory", true))
{
String strHistory = prefs.getString("history", "");
Log.i(CONTENT_TAG, "History loaded");
if (strHistory != null && !strHistory.equals(""))
{
mWordHistory = new ArrayList<String>(Arrays.asList(strHistory.split(",")));
}
else
{
if (mWordHistory == null)
{
mWordHistory = new ArrayList<String>();
}
else
{
mWordHistory.clear();
}
}
}
else
{
if (mWordHistory == null)
{
mWordHistory = new ArrayList<String>();
}
else
{
mWordHistory.clear();
}
}
}
一切工作正常的歷史。
現在我想調整此代碼以保存喜歡的單詞。幾乎每行代碼都是一樣的,唯一不同的是,適應代碼(不onPause()
)被劃歸:
btnAddFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
//The code is here...
}
但它沒有工作,喜歡的話是不是保存在歷史記錄的情況下。
你們能幫忙嗎?非常感謝你。
這真是太好了!你是否[現在可以聊天?](http://chat.stackoverflow.com/rooms/7163/discussion-between-niamh-doyle-and-muhammad-nabeel-arif) – 2012-01-30 11:55:13