Posts tagged SQLite
Android: populate a Spinner from a SQLite database
22Here’s a bit of code to populate a spinner with data from a database. The database table contains info about all the different species of fish within my app. I want the user to choose the species from the spinner and then the ID of that species is then stored as a foreign key within my ‘captures’ table.
First start by defining the spinner in your xml layout, here’s the example from my layout:
<Spinner
android:id="@+id/spinner_species"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:prompt="@string/addcapture_species_prompt" />
From your Activity, within your onCreate() method, get a reference to the spinner and a Cursor to all your species from your database. Pass your cursor into startManagingCursor() to let your Activity take care of it:
Spinner speciesSpinner = (Spinner)findViewById(R.id.spinner_species); Cursor speciesCursor = mDbAdapter.fetchAllSpecies(); startManagingCursor(speciesCursor);
Create a SimpleCursorAdapter to bind the text you want to be displayed on your spinner to the textview display on your spinner and set this to the spinner. SpecimenHunterDatabaseAdapter.KEY_SPECIES_NAME is a String constant of the name of the column in my database containing the name. android.R.layout.simple_spinner_item is a predefined Android layout for the spinner and android.R.id.text1 is the textview defined within this. android.R.layout.simple_spinner_dropdown_item is our layout for the items in the menu that appears when you tap the spinner.
String[] from = new String[] { SpecimenHunterDatabaseAdapter.KEY_SPECIES_NAME };
int[] to = new int[] { android.R.id.text1 };
SimpleCursorAdapter speciesSpinnerAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, speciesCursor, from, to);
speciesSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
speciesSpinner.setAdapter(speciesSpinnerAdapter);
When the user selects an option on the spinner, we need store that somewhere. Create a member in your Activity to hold the value:
private int mSpinnerSpeciesId;
Lastly, back in your onCreate() method below the call to setAdapter() we need to attach an OnItemSelectedListener to the spinner. The method onItemSelected() will be called when the selected item on the spinner changes – it’s also called when your activity starts with the first value on the spinner’s list.
speciesSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Cursor c = (Cursor)parent.getItemAtPosition(pos);
mSpinnerSpeciesId = c.getInt(c.getColumnIndexOrThrow(SpecimenHunterDatabaseAdapter.KEY_SPECIES_ROWID));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Here’s the spinner in action after being pressed:
Android: Get number of rows in SQLite Database Table
4Here’s a little function from my Database Adapter class in the app I’m working on. It returns the number of rows in the “Places” table from my database.
It does this using a SQLiteStatement object, much simpler and cleaner than getting a Cursor using SQLiteDatabase.rawQuery().
A SQLiteStatement can be used for database queries that return “1×1 result sets”, that is one row with one column – a single value.
private static final String DB_TABLE_PLACES = "Places";
private SQLiteDatabase mDatabase;
private long fetchPlacesCount() {
String sql = "SELECT COUNT(*) FROM " + DB_TABLE_PLACES;
SQLiteStatement statement = mDatabase.compileStatement(sql);
long count = statement.simpleQueryForLong();
return count;
}
