列表项的话用
adapter.add(String)
运行截图
点击下拉式列表:
删除列表项的话用adapter.remove(spinner.getSelectedItem().toString());
原理很简单,直接上代码吧:
核心代码流程:
①定义了list集合并将数组数据写入
②定义了适配器Arrayadapter,使用android.R.layout.simple_spinner_item布局格式,并把list数据写入
③添加按钮事件中使用语句:adapter.add(String);
④删除按钮事件中使用语句:adapter.remove(spinner.getSelectedItem().toString());
⑤添加spinner的触发事件:spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()),实现两个抽象方法即可
布局文件:main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/LinearLayout1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <TextView
- android:id="@+id/txtshow"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="" />
- <EditText
- android:id="@+id/bookEdittext"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/btnadd"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="添加书名"
- />
- <Button
- android:id="@+id/btndelete"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="删除书名"
- />
- <Spinner
- android:id="@+id/spinner"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- public class MainActivity extends Activity {
- //定义字符串数组资源:
- private static final String[] books =
- {
- "软件测试方法和技术","个体软件过程","多媒体技术基础",
- "软件项目管理案例教程","C++标准程序库"
- };
- //依次定义布局中的组件,TextView+两个EditText+添加与删除按钮,+Spinner
- //定义适配器ArrayAdapter,存储数据的List集合
- private TextView txtchoose;
- private EditText etxtadd;
- private Button btnadd;
- private Button btndelete;
- private Spinner spinner;
- private ArrayAdapter<String> adapter;
- private List<String> allBooks;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //给List初始化,把数组中的值通过for循环添加到list中
- allBooks = new ArrayList<String>();
- for(int i = 0;i < books.length;i++)
- {
- allBooks.add(books[i]);
- }
- //实例化适配器,里面的参数可以参照UI大全那部分
- adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,allBooks);
- //这个是设置选项卡的显示格式的
- adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- //为各个组件找到对象
- txtchoose = (TextView) findViewById(R.id.txtshow);
- etxtadd = (EditText) findViewById(R.id.bookEdittext);
- btnadd = (Button) findViewById(R.id.btnadd);
- btndelete = (Button) findViewById(R.id.btndelete);
- spinner = (Spinner) findViewById(R.id.spinner);
- //将我们的adapter添加到Spinner中
- spinner.setAdapter(adapter);
- //为添加按钮添加事件
- btnadd.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String newbook = etxtadd.getText().toString();
- //判断添加项是否已经存在
- for(int i = 0;i < adapter.getCount();i++)
- {
- if(newbook.equals(adapter.getItem(i)))
- return;
- }
- //判断添加想是否为空
- if(!newbook.equals(""))
- {
- adapter.add(newbook);
- int position = adapter.getPosition(newbook);
- spinner.setSelection(position);
- //清空用于输入的EditText
- etxtadd.setText("");
- }
- }
- });
- //为删除按钮添加删除事件
- btndelete.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- if(spinner.getSelectedItem() != null)
- {
- //删除spinner的值,根据选项的值来移除
- adapter.remove(spinner.getSelectedItem().toString());
- //将EditText清空
- etxtadd.setText("");
- if(adapter.getCount() == 0)
- {
- txtchoose.setText("");
- }
- }
- }
- });
- //为spinner设置事件OnItemSelectedListener,即点击选项框后的事件
- spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- txtchoose.setText(arg0.getSelectedItem().toString());
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- }
- });
- }
- }
运行截图
点击添加按钮:
点击删除按钮:
是不是很简单呢,这里说下一个方法:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
这个是把列表项设成这种类型的,如果没有这句的话就会变成如下的效果: