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 8296687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:07:04+00:00 2026-06-08T15:07:04+00:00

I was successful in getting a value from the spinner and storing it in

  • 0

I was successful in getting a value from the spinner and storing it in a databse, but at return, I couldn’t set Text into Spinner. How do I do this like the other function of EditText?

This is my code:

public class MainActivity_spinner extends Activity
{
     Button save;
     Button show;
     public void onCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          String[] items = new String[] {" ","Male","Female"};
          final Spinner gender =(Spinner)findViewById(R.id.sex);
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, items);
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          gender.setAdapter(adapter);
          save=(Button)findViewById(R.id.save);
          show=(Button)findViewById(R.id.show);
           //----------------------------------------------------------------    
          save.setOnClickListener(new Button.OnClickListener() {
              public void onClick(View view){
                   ContentValues values = new ContentValues();
                   values.put("sex",gender.getSelectedItem().toString());
                   sql.Insert("db",null,values);
                   sql.Close();
              }
          });
          show.setOnClickListener(new Button.OnClickListener() {
              public void onClick(View view){
                   sql.open();
                   String query = "select sex from db where id=2";
                   Cursor c = sql.rawQuery(query,null);
                   c.moveToFirst();
                   (Error is here)it is my spinner i want show value >>>>>>          gender.setSelection(c.getString(0), true);     
              }
          });
    }
}

I saved the second item into my database from my spinner. Now my spinner is reset. Then, I click on the Show button. The item I saved will show in the spinner as data. Why is this failing?

  • 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-08T15:07:06+00:00Added an answer on June 8, 2026 at 3:07 pm

    If i didn’t get you wrong,you are trying to find the way you can set a text as selected item in spinner,which is one of the items from your database you have list of,in spinner.

    If so,then this might help you:

    Example:

      ArrayList<String> options=new ArrayList<String>();
        ...onCreate(){
          ...
          Cursor c=<your cursor with value>;
          for(loop for total records){
               options.add(<value from cursor>);
          }
          //now you have options with all values of your database
    
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,options);
          mSpinner.setAdapter(adapter); // this will set list of values to spinner
    
          mSpinner.setSelection(options.indexOf(<value you want to show selected>)));//set selected value in spinner
        }
    

    This will be like:

    You have these options in database:

    1.apple

    2.orange

    3.pineapple

    4.strawberry

    5.grapes

    now,you want to show pineapple to be selected,then,

    mSpinner.setSelection(options.indexOf("pineapple")));
    

    this line will set it accordinly.

    EDIT :
    Try with this:

    public class MainActivity_spinner extends Activity
    {
         Button save;
         Button show;
         ArrayList<String> items=new ArrayList<String>();
         Spinner gender;
    
         public void onCreate(Bundle savedInstanceState)
         {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              items.add("Male");
              items.add("Female");
    
              gender =(Spinner)findViewById(R.id.sex);
              ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, items);              
              gender.setAdapter(adapter);
    
              save=(Button)findViewById(R.id.save);
              show=(Button)findViewById(R.id.show);
               //----------------------------------------------------------------    
              save.setOnClickListener(new Button.OnClickListener() {
                  public void onClick(View view){
                       ContentValues values = new ContentValues();
                       values.put("sex",gender.getSelectedItem().toString());
                       sql.Insert("db",null,values);
                       sql.Close();
                  }
              });
              show.setOnClickListener(new Button.OnClickListener() {
                  public void onClick(View view){
                       sql.open();
                       String query = "select sex from db where id=2";
                       Cursor c = sql.rawQuery(query,null);
                       c.moveToFirst();
                       gender.setSelection(items.indexOf(c.getString(0)));     
                  }
              });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this seems like a simple little problem, but I haven't been successful with
This would seem like a pretty common thing to do, but I am having
I am trying to make icon extractor i am successful in getting icon to
After successful setup on initial launch of MySQL, the page prompted me to set
Return Successful Dim user = ravi Dim pass = admin objcmd.CommandText = Select *
After a successful start of Tomcat and a few REST calls I get this
I have this large (and oddly formatted txt file) from the USDA's website. It
I've got a JSON value that has been converted from a JavaScript object using
I'm returning a list of Bookings Class like this: public JsonResult GetEvents() {List<Common.Booking> lst
I am getting this error when my form is completed, despite the fact that

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.