2

I have a Spinner which I want to populate from an ArrayList holding custom objects which itself gets it's data from SQLite. Each object contains multiple fields and I want to get just one of the fields and use that for the spinner entries.

I have gotten so far using this tutorial but it looks like the spinner is populating the whole object instead of just the string I want - the spinner entries look like;

  • getSiteApproaches:com.herbert.cruisespeed.Approach@3e1725c
  • getSiteApproaches:com.herbert.cruisespeed.Approach@6fde965
  • getSiteApproaches:com.herbert.cruisespeed.Approach@57563a

In my activity I 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 the 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 from my SQLiteHelper class

// Fetch all approaches with a specific SiteID
public ArrayList<Approach> getSiteApproaches(int siteid){
    ArrayList<Approach> approaches = new ArrayList<Approach>();
    SQLiteDatabase database = this.getReadableDatabase();

    String selectQuery = "SELECT * FROM " + TABLE_APPROACHES + " WHERE " + KEY_SITE_ID + " = " + siteid;

    Log.e(LOG, selectQuery);

    Cursor c = database.rawQuery(selectQuery, null);

    if (c.moveToFirst()){
    do {
        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 SO but not found any solutions to my specific issue - the spinner is populating but it looks like its holding the whole object, I just want to display the approachname field.

2 Answers 2

4

You should override toString() in Approach class

 @Override
 public String toString() {
    return _approachname.toString();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow - simples. Thanks
0

you can either go for a custom Adapter, overriding getView() which will give you more flexibility on the how the info will be laid down, or you could simply override toString() in your model class, and return the info you want to show. In fact, what you are seeing atm, is the result of default implementation of toString() of the class Object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.