Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the amount of data you want to save is relatively small you can use <a href="http://developer.android.com/reference/android/content/SharedPreferences.html" rel="nofollow">SharedPreferences</a> to save the <code>String</code> data in your <code>onClick</code> method.</p> <pre><code>@Override public void onClick(DialogInterface dialog, int which) { String input = etAddCat.getText().toString(); if(null != input &amp;&amp; input.length() &gt; 0){ listItems.add(input); // Add all string data to List&lt;String&gt; listItem listItem.add(input); arrayAdapter.notifyDataSetChanged(); }else{ Toast.makeText(getApplicationContext(), "Please enter a new category", Toast.LENGTH_LONG).show(); } } </code></pre> <p>When the user leaves your activity, use the <code>onStop()</code> callback method to save your <code>List&lt;Strings&gt;</code> and store it through <code>SharedPreferences</code>.</p> <pre><code>@Override private void onStop() { super.onStop(); SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putString(getResources().getString(R.string.list_of_strings), new HashSet&lt;String&gt;(listItem)); editor.commit; } </code></pre> <p>Using the <code>onStart()</code> callback, initialize your <code>List</code> and <code>SharedPreferences</code>. When the user navigates to your activity, your list will be reinitialized when it was saved via <code>onStop()</code>.</p> <p>Finally, iterate through your list, add your items to your <code>ArrayList', create your</code>ArrayAdapter` and set it to your list.</p> <pre><code>@Override private onStart(){ super.onStart(); SharedPreferences mSharedPreferences; mSharedPreferences = this.getApplicationContext().getSharedPreferences("MyPreferences", 0); List&lt;String&gt; listItems = new ArrayList&lt;String&gt;(mSharedPreferences.getStringSet("ListOfStrings", null)); ListIterator li = listItem.listIterator(0); while (li.hasNext()) { newStatusList.add((String)li.next()); } arrayAdapter = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1, listItems); lv.setAdapter(arrayAdapter); } </code></pre>
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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