Spinner with custom layout doesn't show nothing Android -
in application must use spinner should show 3 items (it,en,pr). must use personalized layout. code in adapter in oncreate method:
holder.spinnerlenguage = (spinner) convertview .findviewbyid(r.id.spinnerlenguage); arraylist<string> spinnerarray = new arraylist<string>(); spinnerarray.add("it"); spinnerarray.add("en"); spinnerarray.add("pr"); final lenguagespinneradapter lenguageadapter = new lenguagespinneradapter( convertview.getcontext(), spinnerarray); holder.spinnerlenguage.setadapter(lenguageadapter); holder.spinnerlenguage .setonitemselectedlistener(new onitemselectedlistener() ... ...
the adapter class is:
private class lenguagespinneradapter extends arrayadapter<string> implements spinneradapter { private arraylist<string> lenguages; public lenguagespinneradapter(context context, arraylist<string> lenguages) { super(context, r.layout.lenguage_item_layout); this.lenguages = lenguages; } @override public view getdropdownview(int position, view convertview, viewgroup parent) { // first let's verify convertview not null if (convertview == null) { // new view inflate new layout layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.lenguage_item_layout, parent, false); } textview lenguage = (textview) convertview .findviewbyid(r.id.textviewlenguage); lenguage.settext(lenguages.get(position).tostring()); return convertview; } }
edit: getcount error is:
07-11 11:09:20.222: e/androidruntime(32564): fatal exception: main 07-11 11:09:20.222: e/androidruntime(32564): java.lang.runtimeexception: unable start activity componentinfo{com.app/com.app.flipmainactivity}: java.lang.illegalstateexception: arrayadapter requires resource id textview 07-11 11:09:20.222: e/androidruntime(32564): @ android.app.activitythread.performlaunchactivity(activitythread.java:2247) 07-11 11:09:20.222: e/androidruntime(32564): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2297) 07-11 11:09:20.222: e/androidruntime(32564): @ android.app.activitythread.access$700(activitythread.java:152) 07-11 11:09:20.222: e/androidruntime(32564): @ android.app.activitythread$h.handlemessage(activitythread.java:1282) 07-11 11:09:20.222: e/androidruntime(32564): @ android.os.handler.dispatchmessage(handler.java:99) 07-11 11:09:20.222: e/androidruntime(32564): @ android.os.looper.loop(looper.java:137) 07-11 11:09:20.222: e/androidruntime(32564): @ android.app.activitythread.main(activitythread.java:5328) 07-11 11:09:20.222: e/androidruntime(32564): @ java.lang.reflect.method.invokenative(native method) 07-11 11:09:20.222: e/androidruntime(32564): @ java.lang.reflect.method.invoke(method.java:511) 07-11 11:09:20.222: e/androidruntime(32564): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1102) 07-11 11:09:20.222: e/androidruntime(32564): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:869) 07-11 11:09:20.222: e/androidruntime(32564): @ dalvik.system.nativestart.main(native method) 07-11 11:09:20.222: e/androidruntime(32564): caused by: java.lang.illegalstateexception: arrayadapter requires resource id textview 07-11 11:09:20.222: e/androidruntime(32564): @ android.widget.arrayadapter.createviewfromresource(arrayadapter.java:386) 07-11 11:09:20.222: e/androidruntime(32564): @ android.widget.arrayadapter.getview(arrayadapter.java:362) 07-11 11:09:20.222: e/androidruntime(32564): 07-11 11:09:20.222: e/androidruntime(32564): @ android.widget.spinner.onmeasure(spinner.java:443) 07-11 11:09:20.222: e/androidruntime(32564): @ android.view.view.measure(view.java:16059) 071 11:09:20.222: e/androidruntime(32564): @ android.widget.relativelayout.measurechildhorizontal(relativelayout.java:681) 07-11 11:09:20.222: e/androidruntime(32564): @ android.widget.relativelayout.onmeasure(relativelayout.java:461) 07-11 11:09:20.222: e/androidruntime(32564): @ android.view.view.measure(view.java:16059) 07-11 11:09:20.222: e/androidruntime(32564): @ android.widget.relativelayout.measurechildhorizontal(relativelayout.java:681) ... ... 07-11 11:09:20.222: e/androidruntime(32564): caused by: java.lang.classcastexception: android.widget.relativelayout cannot cast android.widget.textview 07-11 11:09:20.222: e/androidruntime(32564): @ android.widget.arrayadapter.createviewfromresource(arrayadapter.java:379) 07-11 11:09:20.222: e/androidruntime(32564): ... 29 more
the spinner draw in layout there no items. ideas?
try implement following code instead of one.
private class lenguagespinneradapter extends baseadapter implements spinneradapter { private arraylist<string> lenguages; public lenguagespinneradapter(context context, arraylist<string> lenguages) { this.lenguages = lenguages; } public int getcount() { return lenguages.size(); } public object getitem(int position) { return lenguages.get(position); } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { // new view inflate new layout layoutinflater inflater = (layoutinflater) getapplicationcontext() .getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.get_exercise_activity, parent, false); } textview lenguage = (textview) convertview .findviewbyid(r.id.btnsave); lenguage.settext(lenguages.get(position).tostring()); return convertview; } }
Comments
Post a Comment