Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7174029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:56:53+00:00 2026-05-28T15:56:53+00:00

I am developing a dictionary app. In my app, I assume that user wants

  • 0

I am developing a dictionary app. In my app, I assume that user wants to save favourite words. I have decided to use SharedPreferences to save these values (I am aware that SQLite and files are better but I am stuck to “SharedPreferences”, so keep on with it).

Here below is my code:

@Override
public void onClick(View v) {                                       
    SharedPreferences faves = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    { 
        SharedPreferences.Editor editor = faves.edit();
        editor.putString("favourite", mSelectedDB + "::" + mCurrentWordId + "::" + mCurrentWord + ",");
        editor.commit();    
    }
    Log.i(CONTENT_TAG,"Favourite saved!");

    Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFarvourite, Toast.LENGTH_SHORT);
    toast.show();   
}

The problem is that it does not retain more than one favourite word. I mean only one favourite word is saved and when a new one is added, the previous is erased.

So, how can the above code be edited so that this problem is solved?

Can you guys there help? Thank you very much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:56:53+00:00Added an answer on May 28, 2026 at 3:56 pm

    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.

                MyUtility.addFavoriteItem(this, "Sports");
                MyUtility.addFavoriteItem(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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an Android language dictionary app. I'm thinking of a Favourites button that
I'm developing a feeder application, and i have the option that let the user
I am currently developing a program that uses C#'s Dictionary container (specifically, SortedDictionary). This
I am developing a database-like application that stores a a structure containing: struct Dictionary
We're developing a Windows Phone 7 app and have a TextBlock defined like so:
I am developing a generic layer that allows me to perform the basic save,
I am developing an app that utilizes very large lookup tables to speed up
I'm developing a web app that will access and work with large amounts of
I am developing one application.In that i use the json.Json gives the output and
We are developing a dictionary application for Android. We have bought authorized dictionary contents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.