Note that there are some explanatory texts on larger screens.

plurals
  1. POdynamic spinner and sharedpreferences
    text
    copied!<p>in my android application I have a Button which adds a new dynamic Spinner to the Layout. All of the created Spinners are using the same Array.</p> <p>What is working until now, I can save the number of created Spinners and recreate them after restarting the Application. But I really would like to save the selectedPosition of each Spinner in the sharedPreferences and this is where I'm stucking in a ForceClose Desaster...</p> <p>In my understanding, every Spinner gets an ID when created so you can save the Position bounded on this ID in the preferences.</p> <p>So this is what I did:</p> <pre><code>public void addSpinner(){ LinearLayout AddLayout = (LinearLayout)findViewById(R.id.linearAddScroll); spinner = new Spinner(this); ArrayAdapter&lt;?&gt; adapt = ArrayAdapter.createFromResource(this, R.array.Filter, android.R.layout.simple_spinner_item); adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapt); AddLayout.addView(spinner); } </code></pre> <p>this creates the Spinner.</p> <pre><code>public void onClick(View v) { addSpinner(); int ID = 1000+x; spinner.setId(ID); Toast.makeText(MatReporterActivity.this,"ID" + ID, 5) .show(); x++; } </code></pre> <p>set the ID.</p> <p>This is what I do in the on Create method:</p> <pre><code>x = settings.getInt("xsave", 1); for(y = 1; y &lt; x; y++){ addSpinner(); int ID = 1000+y; Spinner s = (Spinner) findViewById(ID); String ys= Integer.toString(ID); Toast.makeText(MatReporterActivity.this,"ID" +ys, 5) .show(); int yf = settings.getInt(ys, 1); s.setSelection(yf); } </code></pre> <p>And this onStop():</p> <pre><code>SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(this); SharedPreferences.Editor editor = settings.edit(); editor.putInt("xsave", x); for(y = 1; y &lt; x; y++){ int ID = 1000+y; Spinner s2= (Spinner) findViewById(ID); int possS = s2.getSelectedItemPosition(); Toast.makeText(MatReporterActivity.this, "IDStop" + ID, 5) .show(); String ys= Integer.toString(ID); editor.putInt(ys, possS); } editor.commit(); } </code></pre> <p>I think there is a logical Problem in the onCreate Method, but I'm not able to find it, also I didn't find any help in the web how to populate and save dynamically created spinners.</p> <p>So maybe someone has an idea. thanks.</p>
 

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