data binding - Android Spinner databind using array list -
i have array list this:
private arraylist<locations> artist_result = new arraylist<location>(); this location class has 2 properties: id , location.
i need bind arraylist spinner. have tried way:
spinner s = (spinner) findviewbyid(r.id.spinnerspcial); arrayadapter adapter = new arrayadapter(this,android.r.layout.simple_spinner_item, artist_result); s.setadapter(adapter); however, shows object's hexadecimal value. think have set display text , value spinner controller. please can 1 me. need urgently.
the arrayadapter tries display location-objects strings (which causes hex-values), calling object.tostring()-method. it's default implementation returns:
[...] string consisting of the name of class of object instance, the at-sign character `@', , unsigned hexadecimal representation of hash code of object.
to make arrayadadpter show useful in item list, can override tostring()-method return meaningful:
@override public string tostring(){ return "something meaningful here..."; } another way is, extend baseadapter and implement spinneradapter create own adapter, knows elements in arraylist objects , how use properties of objects.
[revised] implementation example
i playing around bit , managed work:
public class main extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // create , display spinner: spinner s = new spinner(this); abslistview.layoutparams params = new abslistview.layoutparams( viewgroup.layoutparams.fill_parent, viewgroup.layoutparams.wrap_content ); this.setcontentview(s, params); // fill arraylist: list<guy> guys = new arraylist<guy>(); guys.add(new guy("lukas", 18)); guys.add(new guy("steve", 20)); guys.add(new guy("forest", 50)); myadapter adapter = new myadapter(guys); // apply adapter: s.setadapter(adapter); // onclicklistener: s.setonitemselectedlistener(new adapterview.onitemselectedlistener() { /** * called when new item selected (in spinner) */ public void onitemselected(adapterview<?> parent, view view, int pos, long id) { guy g = (guy) parent.getitematposition(pos); toast.maketext( getapplicationcontext(), g.getname()+" "+g.getage()+" years old.", toast.length_long ).show(); } public void onnothingselected(adapterview parent) { // nothing. } }); } /** * own adapter implementation displays * arraylist of "guy"-objects. */ private class myadapter extends baseadapter implements spinneradapter { /** * internal data (the arraylist objects). */ private final list<guy> data; public myadapter(list<guy> data){ this.data = data; } /** * returns size of arraylist */ @override public int getcount() { return data.size(); } /** * returns 1 element of arraylist * @ specified position. */ @override public object getitem(int position) { return data.get(position); } @override public long getitemid(int i) { return i; } /** * returns view shown when element * selected. */ @override public view getview(int position, view recycle, viewgroup parent) { textview text; if (recycle != null){ // re-use recycled view here! text = (textview) recycle; } else { // no recycled view, inflate "original" platform: text = (textview) getlayoutinflater().inflate( android.r.layout.simple_dropdown_item_1line, parent, false ); } text.settextcolor(color.black); text.settext(data.get(position).name); return text; } } /** * simple class holds information-fields * guys. */ private class guy{ private final string name; private final int age; public guy(string name, int age){ this.name = name; this.age = age; } public string getname() { return name; } public int getage() { return age; } } } i commented code, if have questions, don't hesitate ask them.
Comments
Post a Comment