如何在Android中向微调器中添加项?

时间:2011-03-09 05:16:41

标签: android spinner

如何将项目添加到微调器?

11 个答案:

答案 0 :(得分:124)

XML文件:

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

Java文件:

public class SpinnerExample extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] arraySpinner = new String[] {
            "1", "2", "3", "4", "5", "6", "7"
        };
        Spinner s = (Spinner) findViewById(R.id.Spinner01);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, arraySpinner);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(adapter);
    }
}

See spinner example

答案 1 :(得分:91)

我找到了另一个选项:布局文件中的微调器定义

<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>

Link to the full source of the code

答案 2 :(得分:29)

试试这段代码:

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 array xml file ..

添加微调器项值
<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
        }
    });

答案 3 :(得分:9)

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

以下是一个例子:

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

答案 4 :(得分:5)

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

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;       
}

答案 5 :(得分:5)

<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item Two</item>
<item>Array Item Three</item>
</string-array>

在你的布局中:

<Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:entries="@array/array_name"
    />

答案 6 :(得分:2)

将微调器添加到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);

答案 7 :(得分:1)

更新列表后添加此代码

假设:

ArrayAdapter&lt; String&gt;变量名是 dataAdapter , 列表变量名称为

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

答案 8 :(得分:1)

要将另外一项添加到 Spinner ,您可以:

checkbox

答案 9 :(得分:0)

只需清除适配器,添加所有itens并通知如下所示的更改:

  public void show(List<Object> objLIst) {
    adapter.clear();
    adapter.addAll(objLIst);
    adapter.notifyDataSetChanged(); }

答案 10 :(得分:0)

更简单的方法是使用材质微调器库:https://github.com/jaredrummler/MaterialSpinner

首先添加到您的项目中:

compile 'com.jaredrummler:material-spinner:1.2.4'

并使用如下:

<com.jaredrummler.materialspinner.MaterialSpinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

和java代码,您可以在java中轻松添加项目:

MaterialSpinner spinner = (MaterialSpinner) findViewById(R.id.spinner);
spinner.setItems("item 1", "item 2", "item 3", "item 4", "item 5");
spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {

  @Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
    Snackbar.make(view, "Clicked " + item, Snackbar.LENGTH_LONG).show();
  }
});