java - Populate Spinner with ArrayList object -
i have spinner want populate arraylist holding custom objects gets it's data sqlite. each object contains multiple fields , want 1 of fields , use spinner entries.
i have gotten far using this tutorial looks spinner populating whole object instead of string want - spinner entries like;
- getsiteapproaches:com.herbert.cruisespeed.approach@3e1725c
- getsiteapproaches:com.herbert.cruisespeed.approach@6fde965
- getsiteapproaches:com.herbert.cruisespeed.approach@57563a
in activity have;
db = new sqlitehelper(getapplicationcontext()); approachlist = db.getsiteapproaches(currentsiteid); arrayadapter<approach> sadapter = new arrayadapter<approach>(this, android.r.layout.simple_spinner_item, approachlist); toolbarspinner.setadapter(sadapter);
and approach class:
package com.cruisespeed.herbert.cruisespeed; public class approach { int _id; int _siteid; string _approachname; int _speedlimit; int _distance; string _createddatetime; // constructors public approach(){ } public approach(int id, int siteid, string approachname, string createddatetime) { this._id = id; this._siteid = siteid; this._approachname = approachname; this._createddatetime = createddatetime; } public approach(int siteid, string approachname, string createddatetime) { this._siteid = siteid; this._approachname = approachname; this._createddatetime = createddatetime; } // getters public int getid(){ return this._id; } public int getsiteid(){ return this._siteid; } public string getapproachname(){ return this._approachname; } public int getspeedlimit(){ return this._speedlimit; } public int getdistance(){ return this._distance; } public string getcreateddatetime(){ return this._createddatetime; } // setters public void setid(int id){ this._id = id; } public void setsiteid(int siteid){ this._siteid = siteid; } public void setapproachname(string approachname){ this._approachname = approachname; } public void setspeedlimit(int speedlimit){ this._speedlimit = speedlimit; } public void setdistance(int distance){ this._distance = distance; } public void setcreateddatetime(string createddatetime){ this._createddatetime = createddatetime; } }
and sqlitehelper class
// fetch approaches specific siteid public arraylist<approach> getsiteapproaches(int siteid){ arraylist<approach> approaches = new arraylist<approach>(); sqlitedatabase database = this.getreadabledatabase(); string selectquery = "select * " + table_approaches + " " + key_site_id + " = " + siteid; log.e(log, selectquery); cursor c = database.rawquery(selectquery, null); if (c.movetofirst()){ { approach ap = new approach(); ap.setid(c.getint(c.getcolumnindex(key_approach_id))); ap.setsiteid(c.getint(c.getcolumnindex(key_site_id))); ap.setapproachname(c.getstring(c.getcolumnindex(key_approach_name))); ap.setspeedlimit(c.getint(c.getcolumnindex(key_approach_speed_limit))); ap.setdistance(c.getint(c.getcolumnindex(key_approach_distance))); ap.setcreateddatetime(c.getstring(c.getcolumnindex(key_created_at))); approaches.add(ap); log.e(log, "getsiteapproaches:" + string.valueof(ap)); // todo debugging spinner } while (c.movetonext()); } c.close(); return approaches; }
i have checked not found solutions specific issue - spinner populating looks holding whole object, want display approachname
field.
you should override tostring()
in approach class
@override public string tostring() { return _approachname.tostring(); }
Comments
Post a Comment