Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After API 11 the <code>SharedPreferences Editor</code> accept <code>Sets</code>. You could convert your List into a <code>HashSet</code> or something similar and store it like that. When your read it back, convert it into an <code>ArrayList</code>, sort it if needed and you're good to go.</p> <pre><code>//Retrieve the values Set&lt;String&gt; set = myScores.getStringSet("key", null); //Set the values Set&lt;String&gt; set = new HashSet&lt;String&gt;(); set.addAll(listOfExistingScores); scoreEditor.putStringSet("key", set); scoreEditor.commit(); </code></pre> <hr> <p>You can also serialize your <code>ArrayList</code> and then save/read it to/from <code>SharedPreferences</code>. Below is the solution:</p> <p><strong>EDIT:</strong><br> Ok, below is the solution to save <code>ArrayList</code> as serialized object to <code>SharedPreferences</code> and then read it from SharedPreferences.</p> <p>Because API supports only storing and retrieving of strings to/from SharedPreferences (after API 11, its simpler), we have to serialize and de-serialize the ArrayList object which has the list of tasks into string.</p> <p>In the <code>addTask()</code> method of the TaskManagerApplication class, we have to get the instance of the shared preference and then store the serialized ArrayList using the <code>putString()</code> method:</p> <pre><code>public void addTask(Task t) { if (null == currentTasks) { currentTasks = new ArrayList&lt;task&gt;(); } currentTasks.add(t); // save the task list to preference SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE); Editor editor = prefs.edit(); try { editor.putString(TASKS, ObjectSerializer.serialize(currentTasks)); } catch (IOException e) { e.printStackTrace(); } editor.commit(); } </code></pre> <p>Similarly we have to retrieve the list of tasks from the preference in the <code>onCreate()</code> method:</p> <pre><code>public void onCreate() { super.onCreate(); if (null == currentTasks) { currentTasks = new ArrayList&lt;task&gt;(); } // load tasks from preference SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE); try { currentTasks = (ArrayList&lt;task&gt;) ObjectSerializer.deserialize(prefs.getString(TASKS, ObjectSerializer.serialize(new ArrayList&lt;task&gt;()))); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } </code></pre> <p>You can get <code>ObjectSerializer</code> class from Apache Pig project <a href="https://github.com/apache/pig/blob/89c2e8e76c68d0d0abe6a36b4e08ddc56979796f/src/org/apache/pig/impl/util/ObjectSerializer.java" rel="noreferrer">ObjectSerializer.java</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload