Ricco Balboa
Ricco Balboa

Reputation: 89

Keep spinner selection when other spinner changes

I am creating an app for a friend of mine (whilst learning Java/Android apps) where his customers will be able to use a series of spinners that will select the price of a particular product.

I am stuck on a bit at the moment with the spinners, I finally get how they all hang together and I have managed to create some code that will set the options of a spinner based on the selection of the previous spinner.

This works until as selection is made on a different spinner. for example. "single speed" is selected on spinner 1, "4" is selected on spinner 2, "hook" is selected on spinner 3, spinner 2 resets.

How do I stop my dynamic spinners from resetting once another spinner is selected?

Eventually I will build up all the spinners to be dynamic because different selections will determine what options should be available.

Thanks in advance. (Appreciate its not brilliantly written, hopefully improvements will come over time).

    public class priceList extends Activity implements AdapterView.OnItemSelectedListener{

    Spinner capacity;
    Spinner speed;
    Spinner hoistSpeed;
    Spinner type;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_price_list);


        //Spinner for Capacity
        capacity = (Spinner) findViewById(R.id.capacityTextView);
        ArrayAdapter adapterCapa=ArrayAdapter.createFromResource(this,R.array.arrayCapacity, android.R.layout.simple_spinner_item);
        capacity.setAdapter(adapterCapa);
        capacity.setOnItemSelectedListener(this);

        //Spinner for the Speed
        speed = (Spinner) findViewById(R.id.speedTextView);
        ArrayAdapter adapterSpd = ArrayAdapter.createFromResource(this,R.array.arraySpeed, android.R.layout.simple_spinner_item);
        speed.setAdapter(adapterSpd);
        speed.setOnItemSelectedListener(this);


        //Spinner for the type
        type = (Spinner) findViewById(R.id.typeTextView);
        ArrayAdapter adapterBeam=ArrayAdapter.createFromResource(this,R.array.arrayType, android.R.layout.simple_spinner_item);
        type.setAdapter(adapterBeam);
        type.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {

        //TextView myText = (TextView) view;
        //Toast.makeText(this, myText.getText(), Toast.LENGTH_SHORT).show();


        hoistSpeed = (Spinner) findViewById(R.id.hoistSpeedTextView);
        ArrayAdapter adapterTypeA=ArrayAdapter.createFromResource(this,R.array.arrayHoistSpeedSingle, android.R.layout.simple_spinner_item);
        hoistSpeed = (Spinner) findViewById(R.id.hoistSpeedTextView);
        ArrayAdapter adapterTypeB=ArrayAdapter.createFromResource(this,R.array.arrayHoistSpeedDouble, android.R.layout.simple_spinner_item);

        if(speed.getSelectedItem().toString().equals("Single Speed"))
        {

            hoistSpeed.setAdapter(adapterTypeA);

        }

        else if(speed.getSelectedItem().toString().equals("Dual Speed"))
        {

            hoistSpeed.setAdapter(adapterTypeB);

        }

        }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

}

Upvotes: 0

Views: 161

Answers (1)

Tosin Onikute
Tosin Onikute

Reputation: 4002

Your question is quite confusing, perhaps you can rephrase

But you can just control or set your next spinner based on what is selected in your current spinner, and do your checks appropriately.

You can write your setOnItemSelectedListener directly, to separate the logic from each other.

String[] speedArr = getResources().getStringArray(R.array.arraySpeed);

    speed.setAdapter(adapter);
            speed.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
                     //Do your checks here, and set the next spinner, or reset the previous one.

                 //This automatically gets your selected value, so no need doing an if else check here again
                 String selectedValue = speedArr[position];
                }

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

                }
            });

Upvotes: 1

Related Questions