Skip to main content

SharedPreferences: History saved but why not Favourite?



I use the following code to save words to History in my dictionary app:







@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();

}

}

}







Everything is working fine with History.





Now I want to adapt this code to save favourite words. Almost every code lines are the same, the only difference is that the adapted code (without onPause() ) is placed under:







btnAddFavourite.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v)

//The code is here...

}







But it is not working and the favourite words are not saved as in the case of History.





Can you guys there help? Thank you very much.


Comments

  1. I am modifying my answer for this StackOverflow link.
    You can save multiple favorites in a single preference by adding numerous favorites in a single string, each favorite item separated by comma. Then you can use convertStringToArray method to convert it into String Array. Here is the full source code.
    Use MyUtility Methods to save multiple favorite items.

    btnAddFavourite.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v)

    MyUtility.addFavoriteItem(MyActivity.this, "Sports");
    MyUtility.addFavoriteItem(MyActivity.this, "Entertainment");
    }


    get String array of all favorites saved

    String[] favorites = MyUtility.getFavoriteList(this);// returns {"Sports","Entertainment"};


    Save these methods in separate Utility class

    public abstract class MyUtility {

    public static boolean addFavoriteItem(Activity activity,String favoriteItem){
    //Get previous favorite items
    String favoriteList = getStringFromPreferences(activity,null,"favorites");
    // Append new Favorite item
    if(favoriteList!=null){
    favoriteList = favoriteList+","+favoriteItem;
    }else{
    favoriteList = favoriteItem;
    }
    // Save in Shared Preferences
    return putStringInPreferences(activity,favoriteList,"favorites");
    }
    public static String[] getFavoriteList(Activity activity){
    String favoriteList = getStringFromPreferences(activity,null,"favorites");
    return convertStringToArray(favoriteList);
    }
    private static boolean putStringInPreferences(Activity activity,String nick,String key){
    SharedPreferences sharedPreferences = activity.getPreferences(Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, nick);
    editor.commit();
    return true;
    }
    private static String getStringFromPreferences(Activity activity,String defaultValue,String key){
    SharedPreferences sharedPreferences = activity.getPreferences(Activity.MODE_PRIVATE);
    String temp = sharedPreferences.getString(key, defaultValue);
    return temp;
    }

    private static String[] convertStringToArray(String str){
    String[] arr = str.split(",");
    return arr;
    }
    }


    If you have to add extra favorites. Then get favorite string from SharedPreference and append comma+favorite item and save it back into SharedPreference.
    * You can use any other string for separator instead of comma.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex