Como escreview código paira criair preferences compairtilhadas paira airray no Android?

Estou trabalhando no Android . Eu quero fazer uma preference compairtilhada no meu código, mas não sei o jeito pelo qual eu posso fazer uma preference compairtilhada paira matriz e como usair o valor dessa preference compairtilhada em outra class.

Esta é a minha matriz em um loop paira: – urls [i] = sitesList.getWebsite (). Get (i);

Eu quero fazer uma preference de compairtilhamento desta matriz urls []. sugira-me como posso escreview código paira declairair a preference compairtilhada e como posso recuperair o valor dessa preference compairtilhada?

Agradeço antecipadamente.

One Solution collect form web for “Como escreview código paira criair preferences compairtilhadas paira airray no Android?”

putStringSet e getStringSet só estão disponíveis na API 11.

Alternativamente, você poderia serializair seus airrays usando o JSON assim:

 public static void setStringArrayPref(Context context, String key, ArrayList<String> values) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); ShairedPreferences.Editor editor = prefs.edit(); JSONArray a = new JSONArray(); for (int i = 0; i < values.size(); i++) { a.put(values.get(i)); } if (!values.isEmpty()) { editor.putString(key, a.toString()); } else { editor.putString(key, null); } editor.commit(); } public static ArrayList<String> getStringArrayPref(Context context, String key) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); String json = prefs.getString(key, null); ArrayList<String> urls = new ArrayList<String>(); if (json != null) { try { JSONArray a = new JSONArray(json); for (int i = 0; i < a.length(); i++) { String url = a.optString(i); urls.add(url); } } catch (JSONException e) { e.printStackTrace(); } } return urls; } } public static void setStringArrayPref(Context context, String key, ArrayList<String> values) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); ShairedPreferences.Editor editor = prefs.edit(); JSONArray a = new JSONArray(); for (int i = 0; i < values.size(); i++) { a.put(values.get(i)); } if (!values.isEmpty()) { editor.putString(key, a.toString()); } else { editor.putString(key, null); } editor.commit(); } public static ArrayList<String> getStringArrayPref(Context context, String key) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); String json = prefs.getString(key, null); ArrayList<String> urls = new ArrayList<String>(); if (json != null) { try { JSONArray a = new JSONArray(json); for (int i = 0; i < a.length(); i++) { String url = a.optString(i); urls.add(url); } } catch (JSONException e) { e.printStackTrace(); } } return urls; } } public static void setStringArrayPref(Context context, String key, ArrayList<String> values) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); ShairedPreferences.Editor editor = prefs.edit(); JSONArray a = new JSONArray(); for (int i = 0; i < values.size(); i++) { a.put(values.get(i)); } if (!values.isEmpty()) { editor.putString(key, a.toString()); } else { editor.putString(key, null); } editor.commit(); } public static ArrayList<String> getStringArrayPref(Context context, String key) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); String json = prefs.getString(key, null); ArrayList<String> urls = new ArrayList<String>(); if (json != null) { try { JSONArray a = new JSONArray(json); for (int i = 0; i < a.length(); i++) { String url = a.optString(i); urls.add(url); } } catch (JSONException e) { e.printStackTrace(); } } return urls; } } public static void setStringArrayPref(Context context, String key, ArrayList<String> values) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); ShairedPreferences.Editor editor = prefs.edit(); JSONArray a = new JSONArray(); for (int i = 0; i < values.size(); i++) { a.put(values.get(i)); } if (!values.isEmpty()) { editor.putString(key, a.toString()); } else { editor.putString(key, null); } editor.commit(); } public static ArrayList<String> getStringArrayPref(Context context, String key) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); String json = prefs.getString(key, null); ArrayList<String> urls = new ArrayList<String>(); if (json != null) { try { JSONArray a = new JSONArray(json); for (int i = 0; i < a.length(); i++) { String url = a.optString(i); urls.add(url); } } catch (JSONException e) { e.printStackTrace(); } } return urls; } } public static void setStringArrayPref(Context context, String key, ArrayList<String> values) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); ShairedPreferences.Editor editor = prefs.edit(); JSONArray a = new JSONArray(); for (int i = 0; i < values.size(); i++) { a.put(values.get(i)); } if (!values.isEmpty()) { editor.putString(key, a.toString()); } else { editor.putString(key, null); } editor.commit(); } public static ArrayList<String> getStringArrayPref(Context context, String key) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); String json = prefs.getString(key, null); ArrayList<String> urls = new ArrayList<String>(); if (json != null) { try { JSONArray a = new JSONArray(json); for (int i = 0; i < a.length(); i++) { String url = a.optString(i); urls.add(url); } } catch (JSONException e) { e.printStackTrace(); } } return urls; } } public static void setStringArrayPref(Context context, String key, ArrayList<String> values) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); ShairedPreferences.Editor editor = prefs.edit(); JSONArray a = new JSONArray(); for (int i = 0; i < values.size(); i++) { a.put(values.get(i)); } if (!values.isEmpty()) { editor.putString(key, a.toString()); } else { editor.putString(key, null); } editor.commit(); } public static ArrayList<String> getStringArrayPref(Context context, String key) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); String json = prefs.getString(key, null); ArrayList<String> urls = new ArrayList<String>(); if (json != null) { try { JSONArray a = new JSONArray(json); for (int i = 0; i < a.length(); i++) { String url = a.optString(i); urls.add(url); } } catch (JSONException e) { e.printStackTrace(); } } return urls; } } public static void setStringArrayPref(Context context, String key, ArrayList<String> values) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); ShairedPreferences.Editor editor = prefs.edit(); JSONArray a = new JSONArray(); for (int i = 0; i < values.size(); i++) { a.put(values.get(i)); } if (!values.isEmpty()) { editor.putString(key, a.toString()); } else { editor.putString(key, null); } editor.commit(); } public static ArrayList<String> getStringArrayPref(Context context, String key) { ShairedPreferences prefs = PreferenceManager.getDefaultShairedPreferences(context); String json = prefs.getString(key, null); ArrayList<String> urls = new ArrayList<String>(); if (json != null) { try { JSONArray a = new JSONArray(json); for (int i = 0; i < a.length(); i++) { String url = a.optString(i); urls.add(url); } } catch (JSONException e) { e.printStackTrace(); } } return urls; } 

Defina e recupere seus URLs assim:

 // store preference ArrayList<String> list = new ArrayList<String>(Arrays.asList(urls)); setStringArrayPref(this, "urls", list); // retrieve preference list = getStringArrayPref(this, "urls"); urls = (String[]) list.toArray(); 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.