Kairi San
Kairi San

Reputation: 249

How to get id from Spinner and it is populated by database

I create a Spinner by populating it by a local database. In my DBHelper I used 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;
}

In my fragment I use this to load up my spinner

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

then after that I was trying to get the ID of it but I only get the position not the ID from the database

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 is just returning the same value of what pos have. How can I get the rowID that is save from the database?

**

I only have 2 column in my services table its id and services_name.

I debug it and saw this

debug result

it only gets my String and not my rowID in the database because my database looks like this

enter image description here

Upvotes: 0

Views: 1501

Answers (1)

kris larson
kris larson

Reputation: 30985

You can get the rowid by changing your query:

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

But then the problem is that you can't use ArrayAdapter.

ArrayAdapter will always use the position as the id. Since you are getting the id from the database, the correct solution for you is a subclass of CursorAdapter. In your case, you can use SimpleCursorAdapter.

So let's change your dbHelper getServices() method to return a 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);
    }

The docs for CursorAdapter say

The Cursor must include a column named "_id" or this class will not work.

so we rename "rowid" to "_id" in the query.

Then it's simple:

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

Upvotes: 2

Related Questions