Android Spinner using ArrayList<String>
2022-09-02 00:26:11
到目前为止,我一直在使用来自XML字符串数组的微调器值,例如:
Spinner karant_sp;
karant_sp = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> karant_adapter = ArrayAdapter.createFromResource(this, R.array.karant_list, android.R.layout.simple_spinner_item);
karant_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
karant_sp.setAdapter(karant_adapter);
karant_sp.setSelection(0);
karant_sp.setOnItemSelectedListener(new select_karant());
我可以使用本地java ArrayList-String作为要在下拉菜单中显示的项目列表吗?
像这样:
Spinner karant_sp;
ArrayList<String> return_likes = new ArrayList<String>();
return_likes.add("Hello");
return_likes.add("world");
karant_sp = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> karant_adapter = ArrayAdapter.createFromResource(this, return_likes, android.R.layout.simple_spinner_item);
karant_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
karant_sp.setAdapter(karant_adapter);
karant_sp.setSelection(0);
karant_sp.setOnItemSelectedListener(new select_karant());
我知道上面的代码不起作用,但我想知道使用ArrayList-string工作的东西。