如何从Spinner获取id,它由数据库填充

时间:2015-07-22 21:35:31

标签: android android-spinner

我通过本地数据库填充R::getAll( 'select * from books WHERE accepted IS NULL ORDER BY update_time DESC LIMIT 100 ' ); 来创建Spinner。 在我的DBHelper中,我使用了List<String>

public List<String> getServices(){
    List<String> labels = new ArrayList<String>();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + TBL_SERVICES;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            labels.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning lables
    return labels;
}

在我的片段中,我使用它来加载我的微调器

private void loadSpinnerData() {

    initialazeDatabase();

    // Spinner Drop down elements
    List<String> lables = dbHelper.getServices();

    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spn_services.setAdapter(dataAdapter);
}

然后在那之后我试图获取它的ID,但我只获得该位置而不是数据库中的ID

spn_services.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int pos, long id) {
           Log.e(TAG, "My service id is " + pos + "My Id is "+ id);

        }

id只返回pos所具有的相同值。如何获取从数据库中保存的rowID?

**

我的服务表中只有2列,其id和services_name。

我调试它并看到了这个

debug result

它只获取我的String而不是数据库中的rowID,因为我的数据库看起来像这样

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以通过更改查询来获取rowid:

String selectQuery = "SELECT rowid, * FROM " + TBL_SERVICES;

但问题是您无法使用ArrayAdapter

ArrayAdapter始终使用该位置作为ID。由于您从数据库中获取了id,因此正确的解决方案是CursorAdapter的子类。在您的情况下,您可以使用SimpleCursorAdapter

因此,让我们更改您的dbHelper getServices()方法以返回Cursor

    public Cursor getServicesCursor() {

        // Select All Query
        String selectQuery = "SELECT rowid AS _id, * FROM " + TBL_SERVICES;

        SQLiteDatabase db = this.getReadableDatabase();
        return db.rawQuery(selectQuery, null);
    }

CursorAdapter的文档说

  

Cursor必须包含名为“_id”的列,否则此类将无效。

所以我们在查询中将“rowid”重命名为“_id”。

然后很简单:

    private void loadSpinnerData() {

        initialazeDatabase();

        // Spinner Drop down cursor
        Cursor servicesCursor = dbHelper.getServicesCursor();

        // map the cursor column names to the TextView ids in the layout
        String[] from = { "services_name" };
        int[] to = { android.R.id.text1 };

        // Creating adapter for spinner
        SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(this, 
              android.R.layout.simple_spinner_dropdown_item,
              servicesCursor, from, to, 0);

        // attaching data adapter to spinner
        spn_services.setAdapter(dataAdapter);
    }