Note that there are some explanatory texts on larger screens.

plurals
  1. POShared Preferences for list
    primarykey
    data
    text
    <p>As a an inexperienced Android programmer I need some help. I'm designing this app for a church that needs an Activity that can record prayer requests (similar to making a to-do list). At this point I can type items into a list, but they don't seem to save and come back after closing using the Shared Preferences excerpt from below, also looking around this site it looks like I can do this without a physical button to press to save.</p> <p>SharedPreferences prayerRequests = getPreferences(Activity.MODE_PRIVATE);</p> <pre><code>ListView myListViewa = (ListView)findViewById(R.id.myListView); SharedPreferences.Editor editor = prayerRequests.edit(); editor.putString("prayers", myListViewa.toString()); editor.apply(); </code></pre> <p>The entire class is here:</p> <pre><code> package com.direction.investor.prayerjournal; import java.util.ArrayList; import android.annotation.TargetApi; import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; public class prayerRequests extends Activity { @TargetApi(Build.VERSION_CODES.GINGERBREAD) public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.prayerrequest); ListView myListView = (ListView)findViewById(R.id.myListView); final EditText myEditText = (EditText)findViewById(R.id.myEditText); final ArrayList&lt;String&gt; prayers = new ArrayList&lt;String&gt; (); final ArrayAdapter&lt;String&gt; aa; aa = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, prayers); myListView.setAdapter(aa); SharedPreferences prayerRequests = getPreferences(Activity.MODE_PRIVATE); ListView myListViewa = (ListView)findViewById(R.id.myListView); SharedPreferences.Editor editor = prayerRequests.edit(); editor.putString("prayers", myListViewa.toString()); editor.apply(); myEditText.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) || (keyCode == KeyEvent.KEYCODE_ENTER)) { prayers.add(0, myEditText.getText().toString()); aa.notifyDataSetChanged(); myEditText.setText(""); return true; } return false; } }); } } </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.
 

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