Android Spinner Cont.
Dynamic additions and deletions of data items in spinner
public class ex04_09 extends Activity
{
private static final string[] Countriesstr = {"Beijing", "Tianjin", "Shanghai City", "Guangzhou"};
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= "New"
>
</Button>
<button
Android:id= "@+id/mybutton_remove"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Remove"
>
</Button>
<spinner
Android:id= "@+id/myspinner"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
>
</Spinner>
</LinearLayout>
Android Spinner Cont.