how to hide selected item from spinner list in android -
i want hide selected item list of spinner questions hide selected item custom spinner list didn't answer. want question in link code is....
toolbar.xml <android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorprimarydark" android:minheight="?attr/actionbarsize" app:popuptheme="@style/themeoverlay.appcompat.light" > <spinner android:id="@+id/spinner_nav" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.v7.widget.toolbar> toolbaractivity <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#c9c9c9" android:orientation="vertical" > <include android:id="@+id/toolbar" layout="@layout/spintoolbar" /> </linearlayout> spinneradapter public class customspinneradapter extends arrayadapter<string> { private context context1; private arraylist<string> data; public resources res; layoutinflater inflater; public customspinneradapter(context context, arraylist<string> objects) { super(context, r.layout.spinner_row, objects); context1 = context; data = objects; inflater = (layoutinflater) context1 .getsystemservice(context.layout_inflater_service); } @override public view getdropdownview(int position, view convertview, viewgroup parent) { return getcustomview(position, convertview, parent); } @override public view getview(int position, view convertview, viewgroup parent) { return getcustomview(position, convertview, parent); } // funtion called each row ( called data.size() times ) public view getcustomview(int position, view convertview, viewgroup parent) { view row = inflater.inflate(r.layout.spinner_row, parent, false); textview tvcategory = (textview) row.findviewbyid(r.id.tvcategory); tvcategory.settext(data.get(position).tostring()); return row; } } spinner_row.xml <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@drawable/spinner_selector" android:orientation="vertical" > <textview android:id="@+id/tvcategory" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:textsize="18sp" /> </relativelayout> spinneracivity public class spintoolbaractivity extends appcompatactivity { private toolbar toolbar; private spinner spinner_nav; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.spintoolbaractivity); toolbar = (toolbar) findviewbyid(r.id.toolbar); spinner_nav = (spinner) findviewbyid(r.id.spinner_nav); if (toolbar != null) { setsupportactionbar(toolbar); getsupportactionbar().setdisplayshowtitleenabled(false); } additemstospinner(); } // add items spinner dynamically public void additemstospinner() { arraylist<string> list = new arraylist<string>(); list.add("top news"); list.add("politics"); list.add("business"); list.add("sports"); list.add("movies"); // custom arrayadapter spinner item layout set popup background customspinneradapter spinadapter = new customspinneradapter( getapplicationcontext(), list); spinner_nav.setadapter(spinadapter); spinner_nav.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> adapter, view v, int position, long id) { // on selecting spinner item string item = adapter.getitematposition(position).tostring(); // showing selected spinner item toast.maketext(getapplicationcontext(), "selected : " + item, toast.length_long).show(); } @override public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
final arraylist<string> lastpressed = new arraylist<>(); final boolean[] isfirsttime = {true}; lastpressed.add(0,string.valueof(0)); // last pressed position lastpressed.add(1,"a"); ... spinner_nav.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> adapter, view v, int position, long id) { ... spinadapter.remove(list.get(position)); if(!isfirsttime[0]){ spinadapter.insert(lastpressed.get(1), new integer(lastpressed.get(0))); } else { isfirsttime[0] = false; } string selected = ((textview) view.findviewbyid(r.id.tv)).gettext().tostring(); lastpressed.set(0, string.valueof(position)); lastpressed.set(1, selected); spinadapter.notifydatasetchanged(); }
Comments
Post a Comment