Android: SharedPreference name of array use as array.
6 replies, posted
I am trying to make a simple way for user of app to remember what color scheme user has selected and chosen.
The way it works: I have an arrays of different color palettes, and they are used to randomly color in buttons for main menu (with no repetition). package com.arl.conversion;
[CODE]import java.util.ArrayList;
import java.util.Arrays;import java.util.Collections;import java.util.List;import android.app.Application;import android.content.Context;import android.content.SharedPreferences;public class ColorCollections{ public static final String PREFS_NAME = "Conversions_arl"; Context context; public ColorCollections(Context context){ this.context = context; } public String GetStringSetting(String setName, String def){ SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0); String setting = settings.getString(setName, def); return setting; } public int GetIntSetting(String setName, int def){ SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0); int setting = settings.getInt(setName, def); return setting; } public boolean GetBolSetting(String setName, boolean def){ SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0); boolean setting = settings.getBoolean(setName, def); return setting; } public void SetBooleanSetting(String setName, boolean value){ SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean(setName, value); editor.commit(); } public void SetStringSetting(String setName, String value){ SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString(setName, value); editor.commit(); } public void SetIntSetting(String setName, int value){ SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putInt(setName, value); editor.commit(); } //Color Palettes. public static String[] Win8 = new String[]{"#008299","#00A0B1","#2672EC","#2E8DEF","#8C0095","#A700AE","#5133AB","#643EBF","#AC193D","#BF1E4B","#D24726","#DC572E","#008A00","#00A600","#094AB2","#0A5BC4"}; public static String[] ModernUrban = new String[]{"#47B6BE","#73CC3E","#FF7149","#FF5063","#FF6999","#233977","#4B9364","#8A8F1D","#A63B52","#B23265","#883E57","#888788","#87A087"}; public List<String> GetColorList(){ boolean random = false; //List<String> colorList = Arrays.asList(GetStringSetting("ColorTheme", "Win8")); List<String> colorList = Arrays.asList(Win8); if(random == true){ Collections.shuffle(colorList); Collections.shuffle(colorList); Collections.shuffle(colorList); Collections.shuffle(colorList); } return colorList; } }[/CODE]
See the lines:
[CODE]//List<String> colorList = Arrays.asList(GetStringSetting("ColorTheme", "Win8"));
List<String> colorList = Arrays.asList(Win8);[/CODE]
The commented out line does not work, pretty sure because it doesn't recognize String having value of Win8 as an array/object name.
Any help? Been stuck witht his for past few hours, I am sure it's something simple.
Also how could I put my SharedPref shortcut functions into a seperate class? And how do I link it? I am very bad at understanding Contexes.
Thank You
[editline]19th August 2013[/editline]
Oh dear mother of god....
Here, I will just use pastebin:
[url]http://pastebin.com/L13dHn1X[/url]
Make a switch statement with GetStringSetting() as the operating variable and depending on the case assign the proper list.
Or if you're feeling fancy, use reflection. Don't forget to wrap this into a try/catch statement.
[CODE]String[] userSetting = (String[]) getClass().getDeclaredField(GetStringSetting("ColorTheme", "Win8")).get(this);
List<String> colorList = Arrays.asList(userSetting);[/CODE]
[QUOTE=Kiririn;41889916]Make a switch statement with GetStringSetting() as the operating variable and depending on the case assign the proper list.[/QUOTE]
Is there no a bit more efficient way? As I plan to extend to color scheme having downloadable additional colors so I would store them in file.
[QUOTE=arleitiss;41889956]Is there no a bit more efficient way? As I plan to extend to color scheme having downloadable additional colors so I would store them in file.[/QUOTE]
I updated my original post. Check it out.
First of all, this seems silly:
[code] boolean random = false;
...
if(random == true){
Collections.shuffle(colorList);
Collections.shuffle(colorList);
Collections.shuffle(colorList);
Collections.shuffle(colorList);
}
return colorList;[/code]
Second of all, it's a bad idea to hard-code colors in Android, try checking out the [url="http://developer.android.com/guide/topics/resources/more-resources.html#Color"]Resource system[/url] (that won't work if you want to make extra colors downloadable of course, but neither will hard-coding.)
Oh and one more thing, I'd recommend adding those SharedPrefs helper functions only if you're going to use them multiple times. If you're only planning to store strings there's no need to add functions for booleans or integers.
[QUOTE=Egonny;41889992]First of all, this seems silly:
[code] boolean random = false;
...
if(random == true){
Collections.shuffle(colorList);
Collections.shuffle(colorList);
Collections.shuffle(colorList);
Collections.shuffle(colorList);
}
return colorList;[/code]
Second of all, it's a bad idea to hard-code colors in Android, try checking out the [url="http://developer.android.com/guide/topics/resources/more-resources.html#Color"]Resource system[/url] (that won't work if you want to make extra colors downloadable of course, but neither will hard-coding.)
Oh and one more thing, I'd recommend adding those SharedPrefs helper functions only if you're going to use them multiple times. If you're only planning to store strings there's no need to add functions for booleans or integers.[/QUOTE]
I will use them a lot in other activities.
[QUOTE=arleitiss;41890982]I will use them a lot in other activities.[/QUOTE]
Fair enough.
This ColorCollections class seems weird though. Preferences should preferably be stored by the Activity, and except for that all this class does is shuffling an array and returning it.
Sorry, you need to Log In to post a reply to this thread.