我如何在Android中将项目添加到微调器?

我怎样才能添加项目到微调?

XML文件:

<Spinner android:id="@+id/Spinner01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

Java文件:

 public class SpinnerExample extends Activity { private String[] arraySpinner; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.arraySpinner = new String[] { "1", "2", "3", "4", "5" }; Spinner s = (Spinner) findViewById(R.id.Spinner01); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arraySpinner); s.setAdapter(adapter); } } 

看到微调的例子 。

我发现另一个选项:布局文件中的微调器定义

 <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:drawSelectorOnTop="true" android:prompt="@string/spin" android:entries="@array/spinnerItems" /> 

文件array.xml中的项目定义:

 <resources> <string-array name="spinnerItems"> <item>item1</item> <item>item2</item> <item>item3</item> <item>item4</item> </string-array> </resources> 

链接到代码的完整源代码

试试这个代码:

 final List<String> list = new ArrayList<String>(); list.add("Item 1"); list.add("Item 2"); list.add("Item 3"); list.add("Item 4"); list.add("Item 5"); final String[] str = {"Report 1", "Report 2", "Report 3", "Report 4", "Report 5"}; final Spinner sp1 = (Spinner) findViewById(R.id.spinner1); final Spinner sp2 = (Spinner) findViewById(R.id.spinner2); ArrayAdapter<String> adp1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp1.setAdapter(adp1); ArrayAdapter<String> adp2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str); adp2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp2.setAdapter(adp2); sp1.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(), list.get(position), Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); sp2.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) { // TODO Auto-generated method stub Toast.makeText(getBaseContext(), str[position], Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); 

您也可以通过String数组xml文件添加微调项目值。

 <resources> <string name="app_name">Spinner_ex5</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">MainActivity</string> <string-array name="str2"> <item>Data 1</item> <item>Data 2</item> <item>Data 3</item> <item>Data 4</item> <item>Data 5</item> </string-array> </resources> 

在mainActivity.java中:

 final Spinner sp3 = (Spinner) findViewById(R.id.spinner3); ArrayAdapter<CharSequence> adp3 = ArrayAdapter.createFromResource(this, R.array.str2, android.R.layout.simple_list_item_1); adp3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp3.setAdapter(adp3); sp3.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) { // TODO Auto-generated method stub String ss = sp3.getSelectedItem().toString(); Toast.makeText(getBaseContext(), ss, Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); 

要在Spinner中添加项目,可以做一件事,尝试创build一个适配器,然后将项目添加/删除到适配器中,然后使用setAdapter()方法轻松地将该适配器绑定到微调器。

这里是一个例子:

 spinner.setAdapter(adapter); adapter.add(item1); adapter.add(item2); adapter.add(item3); adapter.add(item4); adapter.add(item5); adapter.notifyDataSetChanged(); spinner.setAdapter(adapter); 

这个代码基本上读取一个JSON数组对象,并将每一行转换为作为parameter passing的微调器中的一个选项:

 public ArrayAdapter<String> getArrayAdapterFromArrayListForSpinner(ArrayList<JSONObject> aArrayList, String aField) { ArrayAdapter<String> aArrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item); aArrayAdapter.setDropDownViewResource(R.layout.multiline_spinner_dropdown_item); //android.R.layout.simple_spinner_dropdown_item try { for (int i = 0; i < aArrayList.size(); i++) { aArrayAdapter.add(aArrayList.get(i).getString(aField)); } } catch (JSONException e) { e.printStackTrace(); ShowMessage("Error while reading the JSON list"); } return aArrayAdapter; } 

将微调器添加到XML布局,然后将此代码添加到Java文件中:

 Spinner spinner; spinner = (Spinner) findViewById(R.id.spinner1) ; java.util.ArrayList<String> strings = new java.util.ArrayList<>(); strings.add("Mobile") ; strings.add("Home"); strings.add("Work"); SpinnerAdapter spinnerAdapter = new SpinnerAdapter(AddMember.this, R.layout.support_simple_spinner_dropdown_item, strings); spinner.setAdapter(spinnerAdapter); 

更新列表后添加此代码

假设:

ArrayAdapter <String>variables名是dataAdapter ,而Listvariables名是

  • dataAdapter.addAll(键);
  • dataAdapter.notifyDataSetChanged();