如何将SQLite ID附加到android中的Spinner对象?

时间:2011-08-17 22:44:37

标签: android

我想从数据库中提取几个列并将它们添加到微调器中,但我希望行的_id也隐藏在某个地方。以下是部分示例代码:

    public void fillProfileSpinner()
    {
        String query = "SELECT _id,fruit_type,weight,colour FROM fruits;";
        SQLiteDatabase profileDatabase = fruitCakes.database.getReadableDatabase();
        Cursor cursor = profileDatabase.rawQuery(query, null);
        startManagingCursor(cursor);
        try {
            cursor.moveToFirst();
            do
            {
                String fruitType = cursor.getString(cursor.getColumnIndex("fruit_type"));
                double weight = cursor.getString(cursor.getColumnIndex("weight"));
                String colour = cursor.getInt(cursor.getColumnIndex("colour"));
                fruitAdapter.add(colour + " " + weight + " " + fruit_type);
            }
            while(cursor.moveToNext());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        cursor.deactivate();
        cursor.close();
        profileDatabase.close();
}

我相信这是我正在寻找的东西,但我发现它很混乱: Android Spinners, say I pull spinner selections from SQL, can I also add their SQL _id's to the spinner values?

如何在某处获取_id列,以便可以从微调器对象中的某个方法访问?

1 个答案:

答案 0 :(得分:1)

我无法想象您的_ID值尚未直接可用或可通过该位置派生的情况:

  • onListItemClick()的{​​{1}}中,ListActivity参数
  • id的{​​{1}}中,onItemClick()参数
  • 您拥有职位的其他任何地方(例如,OnItemClickListener的{​​{1}}),在id上致电getView()并以此方式检索ListAdapter