java - Checkbox item adds to spinner -


i have 2 checkbox , when checkbox item selected should added spinner. possible that? cant find on google, hoping can save me here :) spinner :

public class checkbox extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_checkbox);      spinner spinner;     spinner = (spinner) findviewbyid(r.id.spinner2);     arrayadapter adapter = arrayadapter.createfromresource(this, r.array.itinerary, android.r.layout.simple_spinner_item);     spinner.setadapter(adapter);  } 

the layout

<checkbox     android:id="@+id/cbsiargao"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:text="surigao"     android:layout_alignparenttop="true"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true"     android:layout_margintop="104dp" />  <spinner     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/spinner2"     android:layout_aligntop="@+id/cbsiargao"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true"     android:layout_marginleft="139dp"     android:layout_marginstart="139dp" />  <checkbox     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="boracay"     android:id="@+id/checkbox"     android:layout_below="@+id/cbsiargao"     android:layout_alignparentleft="true"     android:layout_alignparentstart="true" /> 

you need re-implement spinner adapter. here's quick example:

public class spinneradapterexample implements spinneradapter {     public arraylist<string> items = new arraylist<>();  @override public int getcount() {     return items.size(); }  @override public view getview(int position, view parent, viewgroup viewgroup) {     textview textview = new textview(parent.getcontext());     textview.settext(items.get(position));     return textview; } 

don't forget re-implement other required methods, leave them is.

in checkbox class add follow:

spinneradapterexample adapter = new spinneradapterexample(); spinner.setadapter(adapter); 

then whenever need add spinner write following code:

adapter.items.add("new item"); spinner.notifyondatasetchanged(); 

Comments

Popular posts from this blog

r - how do you merge two data frames the best way? -

How to debug "expected android.widget.TextView but found java.lang.string" in Android? -

php - mySQL problems with this code? -