android Spinner 续

动态增删Spinner中的数据项

public class EX04_09 extends Activity
{
  private static final String[] countriesStr = { "北京市", "天津市", "上海市", "广州市" };
  private TextView myTextView;
  private EditText myEditText;
  private Button myButton_add;
  private Button myButton_remove;
  private Spinner mySpinner;
  private ArrayAdapter adapter;
  private List allCountries;
 
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
   
    setContentView(R.layout.main);
    allCountries = new ArrayList();
    for (int i = 0; i < countriesStr.length; i++)
    {
      allCountries.add(countriesStr[i]);
      }
    
    adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, allCountries);
    adapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   
    myTextView = (TextView) findViewById(R.id.myTextView);
    myEditText = (EditText) findViewById(R.id.myEditText);
    myButton_add = (Button) findViewById(R.id.myButton_add);
    myButton_remove = (Button) findViewById(R.id.myButton_remove);
    mySpinner = (Spinner) findViewById(R.id.mySpinner);
   
    mySpinner.setAdapter(adapter);
   
    myButton_add.setOnClickListener(new Button.OnClickListener()
    {
      @Override
      public void onClick(View arg0)
      {
        String newCountry = myEditText.getText().toString();
       
        for (int i = 0; i < adapter.getCount(); i++)
        {
          if (newCountry.equals(adapter.getItem(i)))
          {
            return;
            }
          }
        if (!newCountry.equals(""))
        {
         
          adapter.add(newCountry);
         
          int position = adapter.getPosition(newCountry);
         
          mySpinner.setSelection(position);
         
          myEditText.setText("");
          }
        }
      });
   
    myButton_remove.setOnClickListener(new Button.OnClickListener()
    {
      @Override
      public void onClick(View arg0)
      {
        if (mySpinner.getSelectedItem() != null)
        {
         
          adapter.remove(mySpinner.getSelectedItem().toString());
         
          myEditText.setText("");
          if (adapter.getCount() == 0)
          {
           
            myTextView.setText("");
            }
          }
        }
      });
   
    mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
    {
      @Override
      public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3)
      {
       
        myTextView.setText(arg0.getSelectedItem().toString());
        }
      @Override
      public void onNothingSelected(AdapterView arg0)
      {
       
      }
      });
    }
  }

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/white"
  >
  <TextView 
  android:id="@+id/myTextView"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/title"
  android:textColor="@drawable/black"
  >
  </TextView>
  <EditText
  android:id="@+id/myEditText"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  >
  </EditText>
  <Button
  android:id="@+id/myButton_add"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  android:text="新增"   
  >
  </Button>
  <Button 
  android:id="@+id/myButton_remove"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="移除"  
  >
  </Button>
  <Spinner
  android:id="@+id/mySpinner"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  >
  </Spinner>
 
 
</LinearLayout>

android Spinner 续的更多相关文章

  1. Xamarin android spinner的使用方法

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  2. Android spinner 样式及其使用详解

    设计与开发首页 > 应用专题 > 移动开发 > 正文> Android spinner 样式及其使用详解 相关文章: Android 开源项目应用程序与框架推荐 Android ...

  3. Android Spinner In Toolbar

    As the title of the post suggest in this tutorial we will see how to have spinner widget inside the ...

  4. Android Spinner 下拉框简单应用 详细注解

    目录 Android Spinner 代码部分 Spinner代码介绍 核心代码 说在最后 @ Android Spinner Spinner 提供下拉列表式的输入方式,该方法可以有效节省手机屏幕上的 ...

  5. android Spinner的简单用法

    上代码: spinner = (Spinner) findViewById(R.id.spinner); tv = (TextView) findViewById(R.id.tv); final Ar ...

  6. Android spinner控件

    spinner控件是Android中下拉控件,现在介绍它两种用法.第一种,从资源文件中获取下拉值:第二种,从代码中获取下拉值. 第一种,首先要在资源文件中把值写好: <?xml version= ...

  7. Android Spinner(级联 天气预报)

    activity_spinner.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

  8. android Spinner的使用

    首先是MainActivity package com.example.spinnertest; import java.util.ArrayList; import java.util.List; ...

  9. Android Spinner列表选择框

    Spinner Spinner是一个下拉列表,通常用于选择一系列可选择的列表项,它可以使用适配器,也可以直接设置数组源. 1.直接设置数组源 在res/values/strings.xml中设置数组源 ...

随机推荐

  1. java操作cookies

    建立一个无生命周期的cookie,即随着浏览器的关闭即消失的cookie,代码如下 HttpServletRequest request HttpServletResponse response Co ...

  2. 无论url请求什么.都可以拼接class类名.实例化.传递get参数-->给当前控制器-->传递给抽象父类-->都交给抽象父类.这个方法去处理call_user_func_array()

    <?phpdefine('DS','/');define('A_PATH',str_replace('\\','/',dirname(__FILE__)).DS); //01获取到主程序目录cl ...

  3. LeetCode OJ 104. Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. how computer boot up?

    The power button activates the power supply in the PC, sending power to the motherboard and other co ...

  5. kubernetes 留言版DEMO

    kubernates hello world1 关闭防火墙 $systemctl disable firewalld $systemctl stop firewalld 2 安装etcd 和 kube ...

  6. 使用SQLCMD在SQLServer执行多个脚本 转载

    出处不明 概述: 作为DBA,经常要用开发人员提供的SQL脚本来更新正式数据库,但是一个比较合理的开发流程,当提交脚本给DBA执行的时候,可能已经有几百个sql文件,并且有执行顺序,如我现在工作的公司 ...

  7. Linux文件系统中硬链接和软链接的区别 (转)

    建立硬链接命令:ln src-link dest-link建立软链接:ln -s src-link dest-link   1. 硬链接是别名,软链接是快捷方式   2. 硬链接和源链接指向同一个i节 ...

  8. 文件描述符和exec() close_on_exec

    #include <fcntl.h> #include <iostream> #include <unistd.h> using namespace std; in ...

  9. Stm32 Bootloader整理

    Stm32 Bootloader整理 一.        基本概念 1.IAP IAP是In Application Programming的首字母缩写,IAP是用户自己的程序在运行过程中对User ...

  10. Anton and Making Potions

    Anton and Making Potions time limit per test 4 seconds memory limit per test 256 megabytes input sta ...