Note that there are some explanatory texts on larger screens.

plurals
  1. POupdate My Spinner from SQlite Database
    primarykey
    data
    text
    <p><strong>I FOUND A SOLUTION FOR RESTORING MY VALUES, SEE MY NEW VERSION of populateFields()</strong> Okay so I have been reading through all the Spinner and SQlite posts on here and cannot seem to find a good answer for what I am looking for so I am posting this scenario. My app has two screens and uses the sqlite database on my device saving a name and weight fields from editTexts as strings like so</p> <pre><code>String eName = name.getText().toString(); String eWeight = weight.getText().toString();// where name and weight are EditTexts </code></pre> <p>and I have two spinners as follows</p> <pre><code>String eReps = spinReps.getSelectedItem().toString(); String eSets = spinSets.getSelectedItem().toString(); </code></pre> <p>Then I call this to add to the database</p> <pre><code>long id = mDbHelper.createExercise(eName, eWeight, eReps, eSets); </code></pre> <p>Here is where my issue is, upon someone selecting to create a new exercise my app crashes because it is trying to populate a spinner incorrectly. Here is what I have currently.</p> <pre><code>private void populateFields(){ if(mRowId != null){ // where mRowId is the selected row from the list Cursor exercise = mDbHelper.fetchExercise(); name.setText(exercise.getString(exercise. getColumnIndexOrThrow(ExerciseDbAdapter.KEY_NAME))); weight.setText(exercise.getString(exercise. getColumnIndexOrThrow(ExerciseDbAdapter.KEY_WEIGHT))); // this is the part that i need help with, I do not know how to restore // the current items spinner value for reps and sets from the database. spinReps.setSelection(exercise.getString(exercise. getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS))); spinSets.setSelection(exercise.getString(exercise. getColumnIndexOrThrow(ExerciseDbAdapter.KEY_SETS))); } </code></pre> <p>I assume I need to use some sort of adapter to restore my list items along with the current value from the database, I am just not sure how. </p> <p>Can someone please help me with this???</p> <p>** BELOW IS MY SOLUTION**</p> <p>I had to move my ArrayAdapters repsAdapter, spinAdapter out of my onCreate() and then implement this new populateFields()</p> <pre><code> private void populateFields(){ if(mRowId != null){ Cursor exercise = mDbHelper.fetchExercise(mRowId); // same as before name.setText(exercise.getString(exercise. getColumnIndexOrThrow(ExerciseDbAdapter.KEY_NAME))); // get the string for sReps and sSets from the database String sReps = exercise.getString(exercise. getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS)); String sSets = exercise.getString(exercise. getColumnIndexOrThrow(ExerciseDbAdapter.KEY_SETS)); // use the strings to get their position in my adapters int r = repsAdapter.getPosition(sReps); int s = setsAdapter.getPosition(sSets); // set their returned values to the selected spinner Items spinReps.setSelection(r); spinSets.setSelection(s); // same as before weight.setText(exercise.getString(exercise. getColumnIndexOrThrow(ExerciseDbAdapter.KEY_WEIGHT))); } } </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. 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