Search code examples
androidxmlsharedpreferencesstoredatastore

how a String Set can be retrieve in Android from SharedPreferences


I have a problem to getting retrieve values from sharedPreferences that stored as a String Set.

Set<String> set = new HashSet<String>();
                set.add("Price: " + String.valueOf(item.getItemPrice()));
                set.add("Quantity: " + String.valueOf(QuantityofItem));
                set.add("Total: " + String.valueOf(BillValue));
                set.add(customizeMsg);
                set.add("Image: " + item.getItemImage());
                editor.putStringSet(item.getItemName(), set);

my StringSet contains these 5 values and i want to retrieve that stored data and retrieve as a list view . any method how it can be perform?


Solution

  • I want to retrieve that stored data and retrieve as a list view.

    #. I guess you are trying to retrieve stored Set data as ArrayList. Use below code to retrieve Set as ArrayList :

    SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    
    // Set
    Set<String> set = sharedPreferences.getStringSet(KEY_ITEM_NAME, new HashSet<String>());
    
    // List
    ArrayList<String> list = new ArrayList<String>(set);
    

    #. If you have stored multiple Set in SharedPreferences then do the same for others. Get Set by using KEY_ITEM_NAME value as item.getItemName().

    #. If you want to show it on ListView then follow the steps described by @CommonsWare