Search code examples
androidsharedpreferencesandroid-preferences

How to save preferences composed of 2 strings and display them?


I am currently creating an android application. This application should allow users to enter a RSS feed URL and save it as a preference. I know how to save basic preferences (Strings, booleans...) using a key/value attributes. My problem here is I want to save as one preference the URL of the RSS feed and the name of the website (e.g http://rss.cnn.com/rss/edition.rss --> CNN top stories). So in this case a single preference is composed with 2 strings and I don't know how to do that.

I found a JSON module making possible to save objects as preferences but I would like to avoid using any modules for this app.

Is it possible to do what I want without external modules and if yes, could you please help me with this issue ?


Solution

  • I feel like you are overthinking this. Instead, consider this approach:

    String rssUrl = "someUrlHere";
    
    String rssWebsite = "Some website name here";
    
    //Now, you can use a delimiter before storing your two values 
    
    String rssUrlAndWebsite = rssUrl + "," + rssWebsite;
    
    //Now you can store this using one key. 
    
    //When you want to read them out, you can use your key to get the value and simply split using the delimiter and there, you will have two values!
    

    I hope this helps;

    Alternatively, you could store the two values in a table!