开发者

Easy way to add a 'None' option to spinner?

开发者 https://www.devze.com 2023-03-25 03:26 出处:网络
So right now I have a Spinner where you select from a list of ingredients. It pulls the list from a database and by default, the first list in the spinner if the first item in the db. The issue is, on

So right now I have a Spinner where you select from a list of ingredients. It pulls the list from a database and by default, the first list in the spinner if the first item in the db. The issue is, once the user selects an item in the spinner, there is no way to then unselect that item. The user can select a different item, but they cant select the entire spinner, lets say, Sauce, to nothing at all.

Is there a way to add a 'None' item in the开发者_如何转开发 spinner, other than have to add a 'None' ingredient to my database with Null or 0 values?


Since you didn't include any source Check this out: It should help

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayList<String> yourIngredients = new ArrayList<String>();
//GET ALL INGREDIENTS
//yourIngredients.add(INGREDIENT)
//FINALLY
yourIngredients.add("NONE");
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, android.R.layout.simple_spinner_item, yourIngredients.toArray());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号