android - Linking two NumberPicker views with onValueChanged causes unpredictable crashes -
i've created dialog containing 2 numberpicker views. first contains list of groups, second contains items selected group:
group group items 1 2: group 2 item 2 [2] [3: group 2 item 3] 3 4: group 2 item 4
i'm hooking in setonvaluechangedlistener in first numberpicker populate second numberpicker.
mnumberpickergroup.setonvaluechangedlistener(new numberpicker.onvaluechangelistener() { @override public void onvaluechange(numberpicker numberpicker, int from, int to) { int size = mdata.getitemsforgroup(to).size(); string[] strings = mdata.getitemtitlesforgroup(to); mnumberpickeritems.setminvalue(1); mnumberpickeritems.setvalue(1); mnumberpickeritems.setmaxvalue(size); mnumberpickeritems.setdisplayedvalues(strings); } });
this works - until, in circumstances, changing group few times can cause crash in numberpicker class, when setting setdisplayedvalues strings.
the error array index out of bounds exception in numberpicker items, line string array passed in. i've set break point in update code above, , verified string array correct size number of items set between min , max value on number picker, has stumped me.
java.lang.arrayindexoutofboundsexception: length=22; index=22 @ android.widget.numberpicker.ensurecachedscrollselectorvalue(numberpicker.java:1768) @ android.widget.numberpicker.initializeselectorwheelindices(numberpicker.java:1583) @ android.widget.numberpicker.setmaxvalue(numberpicker.java:1390) @ uk.co.my.app.fragments.groupmarkuptodialog.updateitempicker(markuptodialog.java:99)
i'm start reading through happens in numberpicker figure out if i'm using wrong, suggestions welcome. "ensurecachedscrollselectorvalue" makes me think need reset numberpicker somehow before updating new data i'm not sure.
can see i'm doing wrong here?
i realise numberpicker not string picker, if has better suggestion how achieve sort of ui i'm ears. otherwise, i'm heading down route of trying implement kind of debouncer, update items picker once activity on group picker complete.
it happens when setdisplayedvalue(string[]) many times.
when next string[]'s length lager current getmaxvalue(), exception happens!
my solution
use
picker.setmaxvalue(0);
before
picker.setdisplayedvalues(stringarr);
my code
citypicker.setmaxvalue(0); try { citypicker.setdisplayedvalues(cityset.toarray(new string[cityset.size()])); } catch (exception e) { log.e("ninja", "citypicker.setdisplayedvalues(citys) occurs error. province " + province); } citypicker.setmaxvalue(cityset.size() - 1);
Comments
Post a Comment