Load Empty Spinner

Asked

Viewed 148 times

0

By generating a Spinner, it brings the first value of ArrayList generated already selected.

How do I bring it "empty" ?

The best option would be to populate it with a function onClick instead of doing it in onCreate, for example ?

1 answer

1


You can add an empty field before popular with the array, this same index being 0 you can also use as validation if the user has not selected any option.

Example:

ArrayList<String> meuArray = new ArrayList<>();
meuArray.add("");

//Popular array

mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0)
            //Não selecionou
        else
            //Selecionou
    }

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

    }
});

I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.