android - Change Spinner entries after EditText changes -
i want change spinner entries after changing edittext: have 4 edittext:
- edit1 - edit2 - edit3 - edit4
i have 2 arrays of spinner entries:
- spinner1 - spinner2
the rules: if write in edit1 , edit2 spinner1; if write in edit3 , edit4 have spinner2; if write in edit1, edit2, edit3 , endit4 have spinner1.
i can't have 1 edittext completed, 2 first, 2 lasts, or 4 edittext.
my spinners in strings.xml:
<string-array name="dureeremboursement"> <item>- choisir -</item> <item>5</item> <item>6</item> <item>7</item> <item>8</item> <item>9</item> <item>10</item> <item>11</item> <item>12</item> <item>13</item> <item>14</item> <item>15</item> <item>16</item> <item>17</item> <item>18</item> <item>19</item> <item>20</item> <item>21</item> <item>22</item> <item>23</item> <item>24</item> <item>25</item> </string-array> <string-array name="dureeremboursementcc"> <item>- choisir -</item> <item>5</item> <item>6</item> <item>7</item> <item>8</item> <item>9</item> <item>10</item> <item>11</item> <item>12</item> </string-array>
i tried 2 ways populate spinner:
editcapitauxrestants.setonfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if (!hasfocus) { arrayadapter<charsequence> adapter0 = arrayadapter.createfromresource(getactivity(), r.array.dureeremboursementcc, android.r.layout.simple_spinner_item); adapter0.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); arrayadapter<charsequence> adapter1 = arrayadapter.createfromresource(getactivity(), r.array.dureeremboursement, android.r.layout.simple_spinner_item); adapter1.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); if (editmensualitestotales.gettext().length() > 0 && editcapitauxrestants.gettext().length() > 0) { spinnerdureeremboursement.setadapter(adapter1); } else { spinnerdureeremboursement.setadapter(adapter0); } } } }); spinnerdureeremboursement.setonitemselectedlistener(new adapterview.onitemselectedlistener() { public void onitemselected(adapterview<?> parentview, view selecteditemview, int position, long id) { arrayadapter<charsequence> adapter0 = arrayadapter.createfromresource(getactivity(), r.array.dureeremboursementcc, android.r.layout.simple_spinner_item); adapter0.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); arrayadapter<charsequence> adapter1 = arrayadapter.createfromresource(getactivity(), r.array.dureeremboursement, android.r.layout.simple_spinner_item); adapter1.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); if (editmensualitestotales.gettext().length() > 0 && editcapitauxrestants.gettext().length() > 0) { spinnerdureeremboursement.setadapter(adapter1); } } public void onnothingselected(adapterview<?> arg0) { } });
the first don't work besause, focus still in edittext when click on spinner, , second because when click on spinner bug: can't scroll spinner.
xml:
<spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinnerdureeremboursement" android:layout_below="@+id/dureeremboursement" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:entries="@array/dureeremboursementcc" android:focusable="true" android:focusableintouchmode="true" android:paddingtop="20dp" android:paddingbottom="20dp" />
i tryed entries in xml , without see if bug came it, still don't work.
i think there 2 solutions: - quit focus when user scrolling; - en focus on spinner.
edit:
i'm in fragment:
ontextchanged:
public class demandegratuite extends android.support.v4.app.fragment { ... @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { //placer ici le code pour connaitre la densite et la resolution de lecran view view = inflater.inflate(r.layout.demande_gratuite, container, false); ... /* editcapitauxrestants.setonfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if (!hasfocus) { arrayadapter<charsequence> adapter0 = arrayadapter.createfromresource(getactivity(), r.array.dureeremboursementcc, android.r.layout.simple_spinner_item); adapter0.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); arrayadapter<charsequence> adapter1 = arrayadapter.createfromresource(getactivity(), r.array.dureeremboursement, android.r.layout.simple_spinner_item); adapter1.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); if (editmensualitestotales.gettext().length() > 0 && editcapitauxrestants.gettext().length() > 0) { spinnerdureeremboursement.setadapter(adapter1); } else { spinnerdureeremboursement.setadapter(adapter0); } } } }); spinnerdureeremboursement.setonitemselectedlistener(new adapterview.onitemselectedlistener() { public void onitemselected(adapterview<?> parentview, view selecteditemview, int position, long id) { arrayadapter<charsequence> adapter0 = arrayadapter.createfromresource(getactivity(), r.array.dureeremboursementcc, android.r.layout.simple_spinner_item); adapter0.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); arrayadapter<charsequence> adapter1 = arrayadapter.createfromresource(getactivity(), r.array.dureeremboursement, android.r.layout.simple_spinner_item); adapter1.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); if (editmensualitestotales.gettext().length() > 0 && editcapitauxrestants.gettext().length() > 0) { spinnerdureeremboursement.setadapter(adapter1); } } public void onnothingselected(adapterview<?> arg0) { } }); */ ... return view; } //apres creation de la vue ont va creer les evenements @override public void onactivitycreated(bundle savedinstancestate) { ... } public void ontextchanged(editable editable) { arrayadapter<charsequence> adapter0 = arrayadapter.createfromresource(getactivity(), r.array.dureeremboursementcc, android.r.layout.simple_spinner_item); adapter0.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); arrayadapter<charsequence> adapter1 = arrayadapter.createfromresource(getactivity(), r.array.dureeremboursement, android.r.layout.simple_spinner_item); adapter1.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); int mensualitestotales = editmensualitestotales.gettext().tostring().length(); int capitauxrestants = editcapitauxrestants.gettext().tostring().length(); if (mensualitestotales > 0 && capitauxrestants > 0) spinnerdureeremboursement.setadapter(adapter1); else { spinnerdureeremboursement.setadapter(adapter0); } } }
answer
spinner spinnerdureeremboursement; edittext editmensualitestotales, editcapitauxrestants, editmensualitestotalescc, editcapitauxrestantscc; string stringmensualitestotales, stringcapitauxrestants, stringmensualitestotalescc, stringcapitauxrestantscc; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { //placer ici le code pour connaitre la densite et la resolution de lecran view view = inflater.inflate(r.layout.demande_gratuite, container, false); spinnerdureeremboursement = (spinner) view.findviewbyid(r.id.spinnerdureeremboursement); editmensualitestotales = (edittext) view.findviewbyid(r.id.editmensualitestotales); editcapitauxrestants = (edittext) view.findviewbyid(r.id.editcapitauxrestants); editmensualitestotalescc = (edittext) view.findviewbyid(r.id.editmensualitestotalescc); editcapitauxrestantscc = (edittext) view.findviewbyid(r.id.editcapitauxrestantscc); stringmensualitestotales = editmensualitestotales.gettext().tostring(); stringcapitauxrestants = editcapitauxrestants.gettext().tostring(); stringmensualitestotalescc = editmensualitestotalescc.gettext().tostring(); stringcapitauxrestantscc = editcapitauxrestantscc.gettext().tostring(); editcapitauxrestants.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void aftertextchanged(editable s) { arrayadapter<charsequence> adapter0 = arrayadapter.createfromresource(getactivity(), r.array.dureeremboursementcc, android.r.layout.simple_spinner_item); adapter0.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); arrayadapter<charsequence> adapter1 = arrayadapter.createfromresource(getactivity(), r.array.dureeremboursement, android.r.layout.simple_spinner_item); adapter1.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); if (editmensualitestotales.gettext().length() > 0 && editcapitauxrestants.gettext().length() > 0) { spinnerdureeremboursement.setadapter(adapter1); } else { spinnerdureeremboursement.setadapter(adapter0); } //stringmensualitestotales = s.tostring(); //setupspinner(stringmensualitestotales, stringcapitauxrestants, stringmensualitestotalescc, stringcapitauxrestantscc); } }); }
private string editstring1,editstring2,editstring3,editstring4; public void oncreate(){ //initialise views edit1.addtextchangedlistener(new textwatcher() { @override public void aftertextchanged(editable s) { editstring1 = s.tostring(); setupspinner(editstring1,editstring2,editstring3,editstring4) }) //do same other edit texts , save them appropriate strings } public void setupspinner(string first,string second,string third,string four){ //check strings , set spinners appropriately }
Comments
Post a Comment