java - populating a spinner with unique strings -
im trying populate action bar spinner unique strings based on user input in dialog box, eg user enters string , if it's not in spinner, should add there. such implementation possible ? tried using arraylist class of course, duplicates there, should use hashset ? thanks
// array of sample strings popluate dropdown list arraylist<string> categories = new arraylist<string>(); // create array adapter popluate dropdown list arrayadapter<string> adapter = new arrayadapter<string>( getbasecontext(), android.r.layout.simple_spinner_dropdown_item, categories); //thats alert dialog through user enters strings alertdialog.builder alert = new alertdialog.builder(this); alert.settitle("new category"); alert.setmessage("please enter new category "); // set edittext view user input final edittext input = new edittext(this); alert.setview(input); alert.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { editable value = input.gettext(); // value! categories.add(value.tostring()); } });
i'd suggest before adding categories check if it's been added before or not using arraylist.contains: http://docs.oracle.com/javase/7/docs/api/java/util/arraylist.html#contains(java.lang.object)
another thing think case sensitivity, perhaps convert lower case before checking if exists in arraylist , adding if doesn't.
Comments
Post a Comment