1. Are you ready for the Galaxy S20? Here is everything we know so far!

OnItemSelected String Value Issue

Discussion in 'Android Development' started by LukeG224, Dec 17, 2013.

  1. LukeG224

    LukeG224 Newbie
    Thread Starter

    I am having a problem with my OnItemSelected on my spinner. I am trying to pass my string value of my first spinner and use it in my second spinner. For some reason, the OnItemSelectedListener is preventing my string values from being used.

    Here is the code for two of my spinners:

    [HIGH]public class PowersportsEquivalent extends Activity {

    DataBaseHelper myDbHelper;
    @SuppressWarnings("deprecation")
    String typePicked = null;
    String makePicked = null;

    @Override
    protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_powersports_equivalent);
    myDbHelper = new DataBaseHelper(this);
    try {
    myDbHelper.createDataBase();
    } catch (IOException ioe) {
    // TODO Auto-generated catch block
    throw new Error("Unable to create database");
    }

    try {
    myDbHelper.openDataBase();
    }catch(SQLException sqle){
    throw sqle;

    }

    final Cursor vType;
    final Cursor vMake;
    final Spinner vTypeSpinner;
    final Spinner vMakeSpinner;=

    //POWERSPORTS TYPE Cursor
    vType = (Cursor) DataBaseHelper.getPowersportsType();
    this.startManagingCursor(vType);

    SimpleCursorAdapter scaType = new SimpleCursorAdapter(this,
    android.R.layout.simple_spinner_item,
    vType,
    new String [] {DataBaseHelper.POWERSPORTS_TYPE},
    new int[] {android.R.id.text1});

    scaType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    vTypeSpinner = (Spinner) findViewById(R.id.typeSpinner);
    vTypeSpinner.setAdapter(scaType);

    //POWERSPORTS MAKE Cursor

    vTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
    position, long id) {
    if(position!=0){

    Cursor vTypeChose = (Cursor)(vTypeSpinner.getSelectedItem());
    if (vTypeChose != null) {
    typePicked = vTypeChose.getString(
    vTypeChose.getColumnIndex(DataBaseHelper.POWERSPORTS_TYPE));
    }
    }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    }
    });
    vMake = (Cursor) DataBaseHelper.getPowersportsMake(typePicked);
    this.startManagingCursor(vMake);

    SimpleCursorAdapter scaMake = new SimpleCursorAdapter(this,
    android.R.layout.simple_spinner_item,
    vMake,
    new String [] {DataBaseHelper.POWERSPORTS_MAKE},
    new int[]{android.R.id.text1});

    scaMake.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    vMakeSpinner = (Spinner) findViewById(R.id.makeSpinner);
    vMakeSpinner.setAdapter(scaMake);[/HIGH]

    I noticed that if I remove the OnItemSelected off my vTypeSpiner, it will pass the string value of the first item in my spinner, but only once because it now cannot listen for a change.

    How can I fix this so every click on my first spinner is listened for and changes the second spinner values?

    If it helps, I am using a database for the values in my spinners. And a quick question about queries. How would I pass multiple string values to a query and use them as arguments?
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...