java - I am trying to create a dynamic spinner -


what want populate area spinner based on city selected first spinner. have attached necessary code. if need else please fell free ask. sorry new this. thank in advance.

select_location_layout.xml

    <?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="#c51162"     android:orientation="vertical" >      <textview         android:id="@+id/textview1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_margintop="80dp"         android:text="@string/selectcity"         android:textappearance="?android:attr/textappearancemedium" />      <spinner         android:id="@+id/cityspinner"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margintop="10dp"         android:spinnermode="dialog" />      <textview         android:id="@+id/textview01"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_margintop="20dp"         android:text="@string/selectarea"         android:textappearance="?android:attr/textappearancemedium" />      <spinner         android:id="@+id/areaspinner"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margintop="10dp"         android:spinnermode="dialog" />  </linearlayout> 

string.xml

    <?xml version="1.0" encoding="utf-8"?> <resources>      <string name="app_name">navigation drawer</string>     <string name="hello_world">hello world!</string>     <string name="action_settings">settings</string>     <string name="drawer_open">open navigation drawer</string>     <string name="drawer_close">close navigation drawer</string>     <string name="selectcity">select city</string>     <string name="selectarea">select area</string>      <string-array name="draweritems">         <item>location</item>         <item>ready delivery</item>         <item>order cake</item>         <item>about us</item>         <item>setting</item>         <item>login</item>         <item>my orders</item>         <item>my cart</item>         <item>help</item>         <item>contact us</item>         <item>rate us</item>         <item>share</item>     </string-array>     <string-array name="cities">         <item>ahmedabad</item>         <item>baroda</item>         <item>surat</item>         <item>mumbai</item>         <item>pune</item>         <item>banglore</item>     </string-array>     <string-array name="ahmedabad_area">         <item>maninagar</item>         <item>c.t.m</item>         <item>c.g. road</item>         <item>satellite</item>         <item>s.g. highway</item>         <item>navarangpura</item>     </string-array>     <string-array name="baroda_area">         <item>alkapuri</item>         <item>fateganj</item>         <item>jaeahar nagar</item>         <item>karali</item>         <item>mandavi</item>         <item>masar road</item>     </string-array>     <string-array name="surat_area">         <item>athwa</item>         <item>bajipura</item>         <item>cenral colony</item>         <item>hajira</item>         <item>kamrej</item>         <item>lajpor</item>     </string-array>     <string-array name="mumbai_area">         <item>adarsha nagar</item>         <item>thane</item>         <item>centacruz</item>         <item>andheri</item>         <item>varsova</item>         <item>dadar</item>     </string-array>     <string-array name="pune_area">         <item>akurdi</item>         <item>bavada</item>         <item>chakan</item>         <item>daund</item>         <item>earth stn</item>         <item>ganeshkhind</item>     </string-array>     <string-array name="banglore_area">         <item>indira nagar</item>         <item>jay nagar</item>         <item>tipsandra</item>         <item>kora mangala</item>         <item>rajaji nagar</item>         <item>white field</item>     </string-array>  </resources> 

locationselection.java

   package com.pixalstudio.navigationdrawer;  import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.spinner; import android.widget.spinneradapter;  public class locationselection extends activity {      protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.select_location_layout);          spinner cityspinner = (spinner) findviewbyid(r.id.cityspinner);         spinner areaspinner = (spinner) findviewbyid(r.id.areaspinner);          arrayadapter<charsequence> adapter = arrayadapter.createfromresource(                 this, r.array.cities, android.r.layout.simple_spinner_item);         adapter.setdropdownviewresource(android.r.layout.simple_dropdown_item_1line);         cityspinner.setadapter(adapter);         final arrayadapter<charsequence> adapter1 = arrayadapter                 .createfromresource(this, r.array.ahmedabad_area,                         android.r.layout.simple_spinner_item);         areaspinner.setadapter(adapter1);          cityspinner                 .setonitemselectedlistener(new adapterview.onitemselectedlistener() {                      @override                     public void onitemselected(adapterview<?> parent,                             view view, int position, long id) {                         adapter1.clear();                          int stringarrayresource = 0;                         switch (position) {                         case 0:                             stringarrayresource = r.array.ahmedabad_area;                             break;                         }                         adapter1.addall(getresources().getstringarray(                                 stringarrayresource));                         adapter1.notifydatasetchanged();                      }                      @override                     public void onnothingselected(adapterview<?> parent) {                         // todo auto-generated method stub                      }                  });     }  } 

logcat

    07-08 09:24:08.274: d/openglrenderer(2879): use egl_swap_behavior_preserved: true 07-08 09:24:08.278: d/(2879): hostconnection::get() new host connection established 0xb42d7740, tid 2879 07-08 09:24:08.754: d/atlas(2879): validating map... 07-08 09:24:09.145: d/libegl(2879): loaded /system/lib/egl/libegl_emulation.so 07-08 09:24:09.146: d/libegl(2879): loaded /system/lib/egl/libglesv1_cm_emulation.so 07-08 09:24:09.156: d/libegl(2879): loaded /system/lib/egl/libglesv2_emulation.so 07-08 09:24:09.166: d/(2879): hostconnection::get() new host connection established 0xaf039490, tid 2897 07-08 09:24:09.204: i/openglrenderer(2879): initialized egl, version 1.4 07-08 09:24:09.266: d/openglrenderer(2879): enabling debug mode 0 07-08 09:24:09.368: w/egl_emulation(2879): eglsurfaceattrib not implemented 07-08 09:24:09.368: w/openglrenderer(2879): failed set egl_swap_behavior on surface 0xaf035800, error=egl_success 07-08 09:24:09.468: i/choreographer(2879): skipped 40 frames!  application may doing work on main thread. 07-08 09:24:18.176: w/egl_emulation(2879): eglsurfaceattrib not implemented 07-08 09:24:18.177: w/openglrenderer(2879): failed set egl_swap_behavior on surface 0xb43c6c20, error=egl_success 07-08 09:24:18.211: d/androidruntime(2879): shutting down vm 07-08 09:24:18.211: e/androidruntime(2879): fatal exception: main 07-08 09:24:18.211: e/androidruntime(2879): process: com.pixalstudio.navigationdrawer, pid: 2879 07-08 09:24:18.211: e/androidruntime(2879): java.lang.unsupportedoperationexception 07-08 09:24:18.211: e/androidruntime(2879):     @ java.util.abstractlist.remove(abstractlist.java:638) 07-08 09:24:18.211: e/androidruntime(2879):     @ java.util.abstractlist$simplelistiterator.remove(abstractlist.java:75) 07-08 09:24:18.211: e/androidruntime(2879):     @ java.util.abstractlist.removerange(abstractlist.java:658) 07-08 09:24:18.211: e/androidruntime(2879):     @ java.util.abstractlist.clear(abstractlist.java:466) 07-08 09:24:18.211: e/androidruntime(2879):     @ android.widget.arrayadapter.clear(arrayadapter.java:258) 07-08 09:24:18.211: e/androidruntime(2879):     @ com.pixalstudio.navigationdrawer.locationselection$1.onitemselected(locationselection.java:35) 07-08 09:24:18.211: e/androidruntime(2879):     @ android.widget.adapterview.fireonselected(adapterview.java:914) 07-08 09:24:18.211: e/androidruntime(2879):     @ android.widget.adapterview.dispatchonitemselected(adapterview.java:903) 07-08 09:24:18.211: e/androidruntime(2879):     @ android.widget.adapterview.access$300(adapterview.java:48) 07-08 09:24:18.211: e/androidruntime(2879):     @ android.widget.adapterview$selectionnotifier.run(adapterview.java:873) 07-08 09:24:18.211: e/androidruntime(2879):     @ android.os.handler.handlecallback(handler.java:739) 07-08 09:24:18.211: e/androidruntime(2879):     @ android.os.handler.dispatchmessage(handler.java:95) 07-08 09:24:18.211: e/androidruntime(2879):     @ android.os.looper.loop(looper.java:135) 07-08 09:24:18.211: e/androidruntime(2879):     @ android.app.activitythread.main(activitythread.java:5254) 07-08 09:24:18.211: e/androidruntime(2879):     @ java.lang.reflect.method.invoke(native method) 07-08 09:24:18.211: e/androidruntime(2879):     @ java.lang.reflect.method.invoke(method.java:372) 07-08 09:24:18.211: e/androidruntime(2879):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) 07-08 09:24:18.211: e/androidruntime(2879):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) 

please me know how can populate areas in spinner2 based on city selected in spinner1.

ok here working sample. took bit longer expected works. idea text of cityspinner , use resource id string array needed areaspinner. code handle item selection placed in onstart event fires once.

mainactivity.java

import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.spinner; import android.widget.adapterview; import android.widget.adapterview.onitemselectedlistener; import android.widget.arrayadapter; import android.content.res.resources; import android.util.log;  public class mainactivity extends activity {    private string tag = "mainactivity";    private activity mactivity;   private spinner mcityspinner;   private spinner mareaspinner;    @override   protected void oncreate(bundle bundle) {     super.oncreate(bundle);      setcontentview(r.layout.activity_main);      log.d(tag, "oncreate");      mactivity = this;      mcityspinner = (spinner)findviewbyid(r.id.cityspinner);     mareaspinner = (spinner)findviewbyid(r.id.areaspinner);      arrayadapter<charsequence> adapter = arrayadapter.createfromresource(this, r.array.cities, android.r.layout.simple_spinner_item);     adapter.setdropdownviewresource(android.r.layout.simple_dropdown_item_1line);     mcityspinner.setadapter(adapter);   }    @override   public void onstart(){     super.onstart();      log.d(tag, "onstart");      mcityspinner.setonitemselectedlistener(new onitemselectedlistener() {         @override         public void onitemselected(adapterview<?> parentview, view selecteditemview, int position, long id) {           log.d(tag, "mcityspinner.onitemselected");            string city = mcityspinner.getselecteditem().tostring();           log.d(tag, string.format("city:%s", city));            string name = string.format("%s_area", city).tolowercase();           log.d(tag, string.format("name:%s", name));            string packagename = getpackagename();           log.d(tag, string.format("packagename:%s", packagename));            resources res = getresources();           int rid = res.getidentifier(name, "array", packagename);           log.d(tag, string.format("rid:%d", rid));            string[] items = res.getstringarray(rid);           arrayadapter<string> adapter = new arrayadapter<string>(mactivity, android.r.layout.simple_spinner_item, items);           mareaspinner.setadapter(adapter);         }          @override         public void onnothingselected(adapterview<?> parentview) {           log.d(tag, "mcityspinner.onnothingselected");            string[] items = new string[]{};           arrayadapter<string> adapter = new arrayadapter<string>(mactivity, android.r.layout.simple_spinner_item, items);           mareaspinner.setadapter(adapter);         }     });    }   } 

activity_main.xml

<?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="#c51162"     android:orientation="vertical" >      <textview         android:id="@+id/textview1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_margintop="80dp"         android:text="@string/selectcity"         android:textappearance="?android:attr/textappearancemedium" />      <spinner         android:id="@+id/cityspinner"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margintop="10dp"         android:spinnermode="dialog" />      <textview         android:id="@+id/textview01"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_margintop="20dp"         android:text="@string/selectarea"         android:textappearance="?android:attr/textappearancemedium" />      <spinner         android:id="@+id/areaspinner"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margintop="10dp"         android:spinnermode="dialog" />  </linearlayout> 

strings.xml

<string name="app_name">navigation drawer</string> <string name="hello_world">hello world!</string> <string name="action_settings">settings</string> <string name="drawer_open">open navigation drawer</string> <string name="drawer_close">close navigation drawer</string> <string name="selectcity">select city</string> <string name="selectarea">select area</string>  <string-array name="draweritems">     <item>location</item>     <item>ready delivery</item>     <item>order cake</item>     <item>about us</item>     <item>setting</item>     <item>login</item>     <item>my orders</item>     <item>my cart</item>     <item>help</item>     <item>contact us</item>     <item>rate us</item>     <item>share</item> </string-array> <string-array name="cities">     <item>ahmedabad</item>     <item>baroda</item>     <item>surat</item>     <item>mumbai</item>     <item>pune</item>     <item>banglore</item> </string-array> <string-array name="ahmedabad_area">     <item>maninagar</item>     <item>c.t.m</item>     <item>c.g. road</item>     <item>satellite</item>     <item>s.g. highway</item>     <item>navarangpura</item> </string-array> <string-array name="baroda_area">     <item>alkapuri</item>     <item>fateganj</item>     <item>jaeahar nagar</item>     <item>karali</item>     <item>mandavi</item>     <item>masar road</item> </string-array> <string-array name="surat_area">     <item>athwa</item>     <item>bajipura</item>     <item>cenral colony</item>     <item>hajira</item>     <item>kamrej</item>     <item>lajpor</item> </string-array> <string-array name="mumbai_area">     <item>adarsha nagar</item>     <item>thane</item>     <item>centacruz</item>     <item>andheri</item>     <item>varsova</item>     <item>dadar</item> </string-array> <string-array name="pune_area">     <item>akurdi</item>     <item>bavada</item>     <item>chakan</item>     <item>daund</item>     <item>earth stn</item>     <item>ganeshkhind</item> </string-array> <string-array name="banglore_area">     <item>indira nagar</item>     <item>jay nagar</item>     <item>tipsandra</item>     <item>kora mangala</item>     <item>rajaji nagar</item>     <item>white field</item> </string-array> 

note: code animated hamburger menu

mainactivity.java

import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.spinner; import android.widget.adapterview; import android.widget.adapterview.onitemselectedlistener; import android.widget.arrayadapter; import android.content.res.resources; import android.util.log; import android.support.v4.widget.drawerlayout; import java.util.arraylist; import java.util.linkedhashmap; import java.util.set; import org.json.jsonarray; import org.json.jsonobject; import android.widget.listview; import android.widget.framelayout; import android.widget.simpleexpandablelistadapter; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v7.app.actionbardrawertoggle;  public class mainactivity extends actionbaractivity {    private string tag = "mainactivity";    private activity mactivity;   private drawerlayout mdrawerlayout;   private listview mlistview;   private string[] mitems;   private actionbardrawertoggle mactionbardrawertoggle;    @override   protected void oncreate(bundle bundle) {     super.oncreate(bundle);      setcontentview(r.layout.activity_main);      log.d(tag, "oncreate");      mactivity = this;      mitems = new string[]{"one", "two", "three"};     mdrawerlayout = (drawerlayout)findviewbyid(r.id.drawer);     mlistview = (listview)findviewbyid(r.id.drawer_list);     mlistview.setadapter(new arrayadapter(mactivity, android.r.layout.simple_list_item_1, android.r.id.text1, mitems));     mactionbardrawertoggle = new actionbardrawertoggle(mactivity, mdrawerlayout, r.string.drawer_open, r.string.drawer_close){       @override       public void ondrawerclosed(view view) {         super.ondrawerclosed(view);         getsupportactionbar().settitle("ondrawerclosed");         invalidateoptionsmenu();         syncstate();       }        @override       public void ondraweropened(view drawerview) {         super.ondraweropened(drawerview);         getsupportactionbar().settitle("ondraweropened");         invalidateoptionsmenu();         syncstate();       }     };     mdrawerlayout.setdrawerlistener(mactionbardrawertoggle);     mactionbardrawertoggle.syncstate();     getsupportactionbar().setdisplayhomeasupenabled(true);     getsupportactionbar().sethomebuttonenabled(true);     mactionbardrawertoggle.syncstate();   } } 

activity_main.xml

  <relativelayout     android:layout_width="match_parent"     android:layout_height="match_parent">      <framelayout       android:id="@+id/drawer_frame"       android:layout_width="match_parent"       android:layout_height="match_parent"        android:layout_centerhorizontal="true"       android:layout_centervertical="true"       />    </relativelayout>    <listview     android:id="@+id/drawer_list"     android:layout_width="240dp"     android:layout_height="match_parent"     android:layout_gravity="left"     android:choicemode="singlechoice"     />  </android.support.v4.widget.drawerlayout> 

styles.xml

<resources>   <style name="appbasetheme" parent="theme.appcompat.light.darkactionbar">   </style>   <style name="apptheme" parent="appbasetheme">     <item name="drawerarrowstyle">@style/drawerarrowstyle</item>   </style>   <style name="drawerarrowstyle" parent="widget.appcompat.drawerarrowtoggle">     <item name="spinbars">true</item>     <item name="color">@android:color/white</item>   </style> </resources> 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -