Package training.droplistaddremove.com;
Import java. util. arraylist;
Import java. util. List;
Import Android. App. activity;
Import Android. App. alertdialog;
Import Android. App. Dialog;
Import Android. content. context;
Import Android. content. dialoginterface;
Import Android. OS. Bundle;
Import Android. Text. editable;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. adapterview;
Import Android. widget. arrayadapter;
Import Android. widget. Button;
Import Android. widget. edittext;
Import Android. widget. spinner;
Import Android. widget. textview;
Public class droplistaddremove extends activity {
Private Static final string [] strarray = {"Beijing", "Shanghai", "Tianjin", "Chongqing "};
Private arrayadapter <string> adapter;
Private button butn_add;
Private button butn_remove;
Private textview textview1;
Private edittext edittext1;
Private spinner myspinner;
Private list <string> allcountries;
Private string addstr;
Private Final int dialog_isexit = 0;
Private Final int dialog_insert = 1;
Private Final int dialog_isnull = 2;
Private Final int dialog_isdelete = 3;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Allcountries = new arraylist <string> ();
// Initialize the Array
For (INT I = 0; I <strarray. length; I ++ ){
Allcountries. Add (strarray [I]);
}
// Set attributes
Adapter = new arrayadapter <string> (this,
Android. R. layout. simple_spinner_item, allcountries );
Adapter
. Setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );
// Obtain the control numbers.
Myspinner = (spinner) findviewbyid (R. Id. myspinner );
Butn_add = (button) findviewbyid (R. Id. mybutton_add );
Butn_remove = (button) findviewbyid (R. Id. mybutton_remove );
Textview1 = (textview) findviewbyid (R. Id. mytextview );
Edittext1 = (edittext) findviewbyid (R. Id. myedittext );
// Assign values to the drop-down list
Myspinner. setadapter (adapter );
// Add button event
Butn_add.setonclicklistener (New button. onclicklistener (){
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Addstr = edittext1.gettext (). tostring ();
// Check whether the input value exists in the current array.
For (INT I = 0; I <strarray. length; I ++ ){
If (addstr. Equals (adapter. getitem (I ))){
Textview1.settext (addstr + "cannot be added ");
// New
// Alertdialog. Builder (droplistaddremove. This). settitle ("repeated values added ")
//. Setmessage ("Your added" + addstr + "already exists and cannot be added ")
//. Setpositivebutton ("OK", null). Show ();
Showdialog (dialog_isexit );
Return;
}
}
If (addstr. Equals ("")){
// New
// Alertdialog. Builder (droplistaddremove. This). settitle ("add value cannot be blank ")
//. Setmessage ("the value you added cannot be blank ")
//. Setpositivebutton ("OK", null). Show ();
Showdialog (dialog_isnull );
Return;
}
// If the current input value is null or not, it cannot be added to the contrary.
If (! Addstr. Equals ("")){
Showdialog (dialog_insert );
}
}
});
// Delete button event
Butn_remove.setonclicklistener (New button. onclicklistener (){
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
If (myspinner. getselecteditem ()! = NULL ){
// String strselect = myspinner. getselecteditem (). tostring ();
// Allcountries. Remove (strselect );
// Textview1.settext ("");
// Edittext1.settext ("");
Showdialog (dialog_isdelete );
}
}
});
/* Add onitemselectedlistener to myspinner */
Myspinner
. Setonitemselectedlistener (new Spinner. onitemselectedlistener (){
@ Override
Public void onitemselected (adapterview <?> Arg0, view arg1,
Int arg2, long arg3 ){
/* Bring the value of the selected myspinner to mytextview */
Textview1.settext (arg0.getselecteditem (). tostring ());
}
@ Override
Public void onnothingselected (adapterview <?> Arg0 ){
}
});
}
Protected dialog oncreatedialog (int id ){
Switch (ID ){
Case dialog_isexit:
Return builddialog (this, dialog_isexit );
Case dialog_insert:
Return builddialog (this, dialog_insert );
Case dialog_isnull:
Return builddialog (this, dialog_isnull );
Case dialog_isdelete:
Return builddialog (this, dialog_isdelete );
}
Return super. oncreatedialog (ID );
}
Private dialog builddialog (context, int ID ){
Alertdialog. Builder = new alertdialog. Builder (this );
Switch (ID ){
Case dialog_isexit:
Builder. settitle ("add duplicate value ");
Builder. setmessage ("the" + addstr + "you added already exists and cannot be added ");
Builder. setpositivebutton ("OK", new dialog. onclicklistener (){
@ Override
Public void onclick (dialoginterface dialog, int which ){
// Todo auto-generated method stub
// Finish ();
Return;
}
});
Break;
Case dialog_insert:
Builder. settitle ("add ");
Builder. setmessage ("are you sure you want to add? ");
Builder. setpositivebutton ("OK", new dialog. onclicklistener (){
@ Override
Public void onclick (dialoginterface dialog, int which ){
// Todo auto-generated method stub
// Finish ();
Allcountries. Add (addstr );
Int position = Adapter. getposition (addstr );
/* Select the position of the spinner in the added value */
Myspinner. setselection (position );
Textview1.settext (addstr );
Edittext1.settext ("");
}
}). Setnegativebutton ("cancel", null );
Break;
Case dialog_isnull:
Builder. settitle ("Empty value added ");
Builder. setmessage ("add value cannot be blank ");
Builder. setpositivebutton ("OK", null );
Break;
Case dialog_isdelete:
Builder. settitle ("delete ");
Builder. setmessage ("are you sure you want to delete it? ");
Builder. setpositivebutton ("OK", new dialog. onclicklistener (){
@ Override
Public void onclick (dialoginterface dialog, int which ){
// Todo auto-generated method stub
String strselect = myspinner. getselecteditem (). tostring ();
Allcountries. Remove (strselect );
Textview1.settext (strselect + "deleted successfully ");
Edittext1.settext ("");
}). Setnegativebutton ("cancel", null );
Break;
}
Return builder. Create ();
}
Public Boolean oncreateoptionsmenu (menu)
{
Super. oncreateoptionsmenu (menu );
Menu. Add (0, 0, 0, "add"). seticon (Android. R. Id. Input );
Menu. Add (0, 1, 0, "delete"). seticon (Android. R. drawable. btn_star_big_off );
Return true;
}
Public Boolean onmenuitemselected (INT featureid, menuitem item)
{
Switch (featureid)
{
Case 0:
Showdialog (dialog_insert );
Break;
Case 1:
Showdialog (dialog_isdelete );
Break;
}
Return super. onmenuitemselected (featureid, item );
}
}