Android Spinner Cont.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.