Domanda

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.

È stato utile?

Soluzione

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
        }
    });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top