android Spinner 续
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>
posted on 2014-09-18 22:38 qingchen1984 阅读(121) 评论(0) 编辑 收藏 举报