使用ArrayList对象填充Spinner

时间:2016-02-06 12:09:02

标签: java android sqlite arraylist

我有一个Spinner,我希望从ArrayList中填充自定义对象,这些对象本身可以从SQLite获取数据。每个对象包含多个字段,我想只获取其中一个字段,并将其用于微调器条目。

到目前为止,我已经使用this教程,但看起来微调器正在填充整个对象而不仅仅是我想要的字符串 - 微调器条目看起来像;

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

在我的活动中,我有;

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);

接近类:

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;
    }
}

来自我的SQLiteHelper类

// 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;
}

我已经检查了SO但没有找到任何解决我特定问题的方法 - 微调器正在填充,但它看起来像是拿着整个对象,我只想显示approachname字段。

2 个答案:

答案 0 :(得分:4)

您应override toString()

Approach class
 @Override
 public String toString() {
    return _approachname.toString();
}

答案 1 :(得分:0)

您可以选择自定义Adapter,覆盖getView(),这可以让您更灵活地了解信息的设置方式,也可以直接覆盖toString()模型类,并返回您要显示的信息。事实上,您所看到的内容是默认实现类toString()的{​​{1}}的结果。