jeudi 5 novembre 2015

Android, SQLite, Spinner

how I can get ID of selected item in Spinner from Database?

I know it's onItemSelected method, but I don't how to use it.

AddActivity:

spSpinner.setOnItemSelectedListener(
            new OnItemSelectedListener(){

                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {



                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            }
    );

DatabaseHandler:

public int getId(int id){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_DEPARTMENTS, new String[] {ID_COLUMN, KEY_DEPARTMENT}, ID_COLUMN+"=?", new String[] { String.valueOf(id) }, null);
    if(cursor!=null){
        if(cursor.getCount() > 0){
            cursor.moveToFirst();
        }
    }

    int s = cursor.getInt(cursor.getColumnIndex(ID_COLUMN));
    cursor.close();
    db.close();
    return s;
}

Aucun commentaire:

Enregistrer un commentaire