Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It doesn't look like you are monitoring for changes in the shared preferences. see:</p> <p><a href="http://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html" rel="nofollow">http://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html</a></p> <pre><code>final SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() { public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { // update your listview. } }; prefs.registerOnSharedPreferenceChangeListener(listener); </code></pre> <p>So when you get that callback, it will tell you what key changed. Then you could update the adapter for your listview to have the new contents and call onDataSetChanged() in the adapter.</p> <p>Alternatively, you could move your adapter setting code into the onResume() function. This would make it so when your activity resumes, you check the status of the shared preferences and set the adapter. Be warned though, if the user has scrolled down the list some distance and you call setAdapter() again in the resume, they will lose their scroll position.</p> <p>EDIT:</p> <p>Try:</p> <p>Updater (this shouldn't need to start a new activity, this can just finish()):</p> <pre><code>import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.View; import android.widget.Button; import android.widget.EditText; public class WorldCreator extends Activity{ EditText worldNameEditor; Button saver; SharedPreferences prefs; OnSharedPreferenceChangeListener listener; String updater; Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_worldcreator); prefs = PreferenceManager.getDefaultSharedPreferences(this); worldNameEditor = (EditText) findViewById(R.id.hello); saver = (Button) findViewById(R.id.button1); updater = worldNameEditor.getText().toString() + ","; saver.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editor = prefs.edit(); editor.putString("worldString", updater); editor.commit(); finish(); }}); } </code></pre> <p>ListView:</p> <pre><code>import android.app.ListActivity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; public class WorldMenu extends ListActivity{ SharedPreferences prefs = null; String splitter; String[] worldList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); prefs = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() { public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { // update your listview. } }; prefs.registerOnSharedPreferenceChangeListener(listener); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); splitter = "Create World," + prefs.getString("worldString", "hello"); worldList = splitter.split(","); setListAdapter(new ArrayAdapter&lt;String&gt;(WorldMenu.this, android.R.layout.simple_list_item_1, worldList)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); if(position == 0){ Intent openWorldNamer = new Intent(""); startActivity(openWorldNamer); } } } </code></pre>
    singulars
    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