码迷,mamicode.com
首页 > 移动开发 > 详细

android Spinner 续

时间:2014-09-18 23:41:34      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:android   style   http   color   io   os   ar   for   数据   

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 续

标签:android   style   http   color   io   os   ar   for   数据   

原文地址:http://www.cnblogs.com/qingchen1984/p/3980237.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!