samedi 6 février 2016

Populate Spinner with ArrayList object

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.

Aucun commentaire:

Enregistrer un commentaire