Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set data in an Android spinner?

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?

like image 556
user1551503 Avatar asked Jul 26 '12 11:07

user1551503


1 Answers

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)));     
              }
          });
    }
}
like image 199
Hiral Vadodaria Avatar answered Sep 21 '22 13:09

Hiral Vadodaria