android - Error "java.lang.String cannot be cast to java.lang.Integer" In Custom Adapter -
i have problem custom adapter put hint in spinner. i've made class adapterspinnerhint extends arrayadapter:
public class adapterspinnerhint extends arrayadapter { int labelhint; int textviewid; int layout; arraylist<string> mitems; context context; public adapterspinnerhint(context context, int spinner_layout, int field, arraylist<string> list, int label) { super(context, spinner_layout, list); textviewid = field; labelhint = label; layout = spinner_layout; mitems=list; } @override public view getview(int position, view convertview, viewgroup parent) { view v; layoutinflater inflater = (layoutinflater)context.getsystemservice(context.layout_inflater_service); v = inflater.inflate(layout, null); if (position == getcount()) { ((textview)v.findviewbyid(textviewid)).settext(labelhint); ((textview)v.findviewbyid(textviewid)).sethint((integer) getitem(getcount())); //"hint displayed" } return v; } @override public int getcount() { return mitems.size()-1; // dont display last item. used hint. }
and in activity i've created function create spinner
private void createspinnercustomer(jsonarray customers) { spinner spinner = (spinner) findviewbyid(r.id.customers_spinner); arraylist<string> customerslist=new arraylist<string>(); for(int i=0;i<customers.length();i++){ try { customerslist.add(customers.getjsonobject(i).getstring("name")); } catch (jsonexception e) { e.printstacktrace(); } } customerslist.add(string.valueof(r.string.customer_label)); assert spinner != null; adapterspinnerhint adapter=new adapterspinnerhint( getapplicationcontext(), r.layout.spinner_layout, r.id.txt, customerslist, r.string.customer_label); spinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { public void onitemselected(adapterview<?> parent, view view, int pos, long id) { } public void onnothingselected(adapterview<?> parent) { } }); spinner.setadapter(adapter); spinner.setselection(adapter.getcount()); }
i create general adapter spinner include hint. i'm getting error on adapterspinnerhint in line:
java.lang.classcastexception: java.lang.string cannot cast java.lang.integer
in function - getview see line - .sethint((integer)getitem(getcount()));
.sethint(string) requires string parameter , passing integer parsing string integer.
do this
.sethint(getitem(getcount));
when calling super in constructor of arrayadapter , passing list of strings parameter, telling arrayadapter type of objects in list string. when getitem(), returning string
Comments
Post a Comment