Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinner based on entry field

I am creating a spinner displaying numbers, but as the number could be up to 256 I don't want to have to have all entries 1-256 in my code. The number will be based on a entry field on a different class.

I.E if the user enters 16, the spinner will display number 1 to 16.

I am completely new to spinners so could some please talk in dummy terms if possible.

If this is not possible please help me with another solution.

Much appreciated.

like image 484
Chris James Hancocks Avatar asked Sep 03 '26 11:09

Chris James Hancocks


1 Answers

I'm assuming you want a way to add a Spinner programatically to your app.

Lets say you store your 16 items in an ArrayList

ArrayList<String> spinnerArray = new ArrayList<String>();

Spinners can be created using ArrayAdapters

Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter (this,android.R.layout.simple_spinner_dropdown_item,spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);

So far so good you've got a dynamic spinner but now you need data from it. Here's how you get it:

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onNothingSelected(AdapterView<?> parent) {}
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
          //Your logic for what happens when an Item is selected
        }
    });
like image 181
cjds Avatar answered Sep 06 '26 00:09

cjds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!