Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8350177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:14:12+00:00 2026-06-09T08:14:12+00:00

I FOUND A SOLUTION FOR RESTORING MY VALUES, SEE MY NEW VERSION of populateFields()

  • 0

I FOUND A SOLUTION FOR RESTORING MY VALUES, SEE MY NEW VERSION of populateFields()
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

String eName = name.getText().toString();
String eWeight = weight.getText().toString();// where name and weight are EditTexts

and I have two spinners as follows

String eReps = spinReps.getSelectedItem().toString();
String eSets = spinSets.getSelectedItem().toString();

Then I call this to add to the database

long id = mDbHelper.createExercise(eName, eWeight, eReps, eSets);

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.

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))); 
}

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.

Can someone please help me with this???

** BELOW IS MY SOLUTION**

I had to move my ArrayAdapters repsAdapter, spinAdapter out of my onCreate()
and then implement this new populateFields()

 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)));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:14:13+00:00Added an answer on June 9, 2026 at 8:14 am

    The way to set a Spinner to a value, not a position, depends on what adapter you are using.

    1. ArrayAdapter, this one is easy:

      int position = adapter.getPosition(exercise.getString(exercise.
                       getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS)));
      spinReps.setSelection(position);
      
    2. SimpleCursorAdapter, this one is a little harder:

      String match = exercise.getString(exercise.getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS));
      
      Cursor cursor = adapter.getCursor();
      cursor.moveToPosition(-1);
      int columnIndex = cursor.getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS);
      while(cursor.moveToNext()) {
          if(cursor.getString(columnIndex).equals(match))
              break;
      }
      spinReps.setSelection(cursor.getPosition());
      

    If you are using a different type of adapter and can’t modify the code above to fit it, let me know. Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to found solution to improve phpbb3 new message indexing. I have about
Update Solution Found See Bottom of post if interested Seems simple enough and for
I apologize for the newb question but have not found a solution online. I
Is it possible? I have found solution for Visual Studio Print n levels of
I have html code: <span id=gruszka1>1</span> I found solution to check if that specific
I have a problem to add custom methods to my models. I found solution
I have tried the solution found in this post to disable ARC in AFNetworking
Seems, it's common task, but I haven't found solution. I need to calculate number
Edited: SOLUTION FOUND. This is strange and not the best solution, but I just
I found the solution for this question in C# , but I can't translate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.