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. hdu_4539_郑厂长系列故事——排兵布阵(状压DP|最大团)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 题意:中文,不解释 题解:将每一行的状态压缩,然后进行DP,也可以用最大团做.这里我用的DP # ...

  2. LeetCode OJ 219.Contains Duplicate 2

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  3. diff and patch

    A patch captures the changes of two different files (oldfile and newfile). Given the oldfile and the ...

  4. 设置标题小图标ico

    在head里添加 <link rel="shortcut icon" href="<%=request.getContextPath()%>/FlatU ...

  5. Hibernate4 占位符(?)

    Hibernate3使用?占位符: Session session = sessionFactory.getCurrentSession();  session.beginTransaction(); ...

  6. include和 merge

    include和merge标记的作用主要是为了解决layout的重用问题. 比如我们有三四个Activity但是他们都要用到同一个样式的标题栏,虽然我们把一样的代码copy个三四遍也没关系,但实在是太 ...

  7. JSON中的特殊字符

    使用JSON从后台向前台传输数据的时候,当数据本身含有一些特殊字符,会导致JSON数据的解析出错.这个时候,就需要将JSON中的特殊字符过滤掉. 用下面的方法对即将向前台输出的json字符串进行处理, ...

  8. Validation of viewstate MAC failed machinekey生成、使用方法

    前段时间公司为了减轻服务器压力,对网页做了集群,分布在多台服务器,通过DNS轮回解析到各台服务器,结果页面只要打开停留到DNS解析到下一个地址,就会出现出下错误信息. Validation of vi ...

  9. ./encrypt: error while loading shared libraries: libcrypto.so.10:

    ./encrypt: error while loading shared libraries: libcrypto.so.10:

  10. 转 Linux下的GoldenGate的启动关闭Shell脚本(独立)

    用户想要用OGG进行同步数据,原来用的是Shareplex,至于为啥要换OGG,BulaBula一堆原因.....这不是我们要在意的事情,和客 户装完配置好OGG之后,测试中,客户提出要有个简单的启动 ...