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

  • SEARCH
  • Home
  • 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 8831267
In Process

The Archive Base Latest Questions

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

Found my problem check under Update for the code Okay, I have a problem

  • 0

Found my problem check under “Update” for the code

Okay, I have a problem loading the right value from Sqlite into my Spinner. Here is how my app is constructed

my onCreate() sets up a spinner called spinTypes by:

public class MyClass extends Activity{
 // local members . . . 
     .
     .
     .
 //spinner 
 private Spinner spinTypes, spinNames = null;

 // string array decl.
 private String [] types, names1, names2, names3 = null;

 // array adapters for string arrays
 private ArrayAdapter<String> typesAdapter, names1Adapter,
                             names2Adapter, names3Adapter;

 // create a Dbhelper object to access the db
 private DbHelper mdbHelper = null;

// on create
 @Override
 public void onCreate(Bundle savedIstanceState){
  // call to super class
  // set my content view
  // instantiate all members 

  // Dbhelper object to access the db
  mdbHelper = new DbHelper(getBaseContext());

  // spinners
  spinTypes = (Spinner) findViewById(R.id.spin_types);
  spinNames = (Spinner) findViewById(R.id.spin_names);

 // get string arrays from resources and create a ArrayAdapter<string>
 types = getResources().getStringArray(R.array.types);

 typesAdapter = new ArrayAdapter<String>(getBaseContext(),
                                        android.R.layout.simple_spinner_item, types);
 typesAdapter. setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

 // want to create a dynamic spinner based on these arrays
 names1 = getResources().getStringArray(R.array.names_1); 
 names2 = getResources().getStringArray(R.array.names_2);
 names3 = getResources().getStringArray(R.array.names_3);

 // do this for all names++ arrays
 names1Adapter = new ArrayAdapter<String>(getBaseContext(),
                                        android.R.layout.simple_spinner_item, names1);
 names1Adapter. setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

 // etc . . . for the other two

 // set the adapter for the types spinner
 spinTypes.setAdapter(typesAdapter);

}

I have a creation method for inserting all my data into the db that works fine, my problem is when i want to try and populate the spinners for editing an entry.

The app works as so: you press an add button to popup a dialog that creates a list item in the calling view. I use two spinners to create it, one for the type which dynamically populates the second names spinner based on the type.

when i access the database object i return the strings from the spinner but for some reason when i create my if statement checking the position of the string in the array the second spinner sets position 0 every time.

the method including the if statement i mentioned above is as so:

public void populate(){
  mdbHelper.open();
  // if there is an item create a cursor rowId != null
  Cursor mTypes = mdbHelper.fetchItem(rowId);
   if(mTypes.moveToFirst()){
   String tType = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_TYPE));
   String tName = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_NAME));

  int t = spinTypes.getPosition(tType);
  spinTypes.setSelection(t);

  // ** this does not seem to do the job i want it to**
  if(t == 0){
    spinNames.setAdapter(names1Adapter); // set the adapter based on t value
    int n1 = names1Adapter.getPosition(tName); // use name return from db to get pos
    spinNames.setSelection(n1);            // set the position
  }else if(t ==1){
    spinNames.setAdapter(names2Adapter);
    int n2 = names1Adapter.getPosition(tName);
    spinNames.setSelection(n2);
  }else{
    spinNames.setAdapter(names3Adapter);
    int n3 = names3Adapter.getPosition(tName);
    spinNames.setSelection(n3);
  }

}// end populate

} // end class

The spinners all work but they do not end up on the value with tName i am returning.

Can anyone help with this?

** I am logging the name returned from the db and using it to find the index of the item in my error and i return the correct value, yet the spinner still defaults to 0**

Anyone know what my error may be? please

Update(final code posted below)
I made quite a bit of changes to get this to work, in order to understand what i did you should read this whole post so you don’t miss anything!

added a variable before onCreate()
private int spinNamePos;

added this line after my last line of the onCreate()

// add a listener to the spinner
spinTypes.setOnItemSelectedListener(TypesListener);

and created this listener as so

OnItemSelectedListener TypesListener = new OnItemSelectedListener(){
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
    // use the position of this spinner to set the second spinner
     switch(pos){
     case 0 :
        spinNames.setAdapter(names1Adapter);
        break;
      //etc. .  for the other two
     case 1:
        // etc
       break;
     }
     case 2:
        // etc
       break;
     }
     // created an int for the position of the second spinner before calling onCreate()
     // and use it to set this spinners position
     spinNames.setSelection(spinNamePos); 
 // this value is grabbed in onSavedInstanceState() and used to set the pos when onRestoreInstanceState() is called to handle orientation changes and activity paused
  }
 };

i then changed my onPopulate() to work as so calling a new method to set the second spinner’s position

 public void populate(){
  mdbHelper.open();
  // if there is an item create a cursor rowId != null
  Cursor mTypes = mdbHelper.fetchItem(rowId);
   if(mTypes.moveToFirst()){
   String tType = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_TYPE));
   String tName = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_NAME));

  int t = spinTypes.getPosition(tType);
  spinTypes.setSelection(t);

  // call to the new method
  spinNamePos = setSpinNamesPos( t, tName);
  // set the position
  spinNames.setSelection(spinNamePos)


}// end populate

// new method to set the position when i want to update/change the list item

private int setSpinNamesPos(int m, String name){
 int pos = 0;
 switch(m){
  case 0:
    pos = names1Adapter.getPosition(name);
    break;
  case 1:
    pos = names2Adapter.getPosition(name);
    break;
   case 2:
    pos = names3Adapter.getPosition(name);
    break;
  }
 return pos;
}

So happy to see this working any questions or comments are encouraged

  • 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-14T08:16:18+00:00Added an answer on June 14, 2026 at 8:16 am

    Okay so I have seemed to figure out my own answer, weeks later! The problem I was having is that i was unaware that my OnItemSelectedListener() gets called every time my adapter is set for my first spinner.

    So now realizing this, i set the first spinner in OnItemSelectedListener() then use this spinner id to set my second spinner adapter.(i.e based on the id I set the adapter of my second spinner to 1 of 3 choices.)

    After this adapter is set I use the value returned from my cursor to find the string in this adapter and set the spinner to the position of the string in my array.

    It all works great now and I will be putting the final code above under the UPDATE title. Thanks for all help.

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

Sidebar

Related Questions

i have big problem with explanation code under, meanwhile i made two loops function
We have encountered a very strange class not found problem in our web app
Seem to run into a service endpoint not found problem when trying to get
I'am currently working on GWTs Activity-Place implementation. Now I have found problem with the
I found some problem. When i running apc_store and more times update a page
I have two branches: trunk, production. I have found a problem in trunk, made
I have a project that is C++ WIN32 project. I found a problem that
We have found a problem with our deployment to a production server that runs
Update: I found the problem, I output the sql statement to a file instead
My problem is that I have written code in Visual C# for a background

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.