Reputation: 1228
I am using a SimpleCursorAdapter to populate a Spinner with the name column from a database.
Adapter:
spinnerAdapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
null,
new String[] {SupplierEntry.COLUMN_SUPPLIER_NAME},
new int[] {android.R.id.text1},
0);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
mSuppliersSpinner.setAdapter(spinnerAdapter);
getLoaderManager().initLoader(SUPPLIERS_LOADER, null, this);
Cursor Loader:
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
// Define a projection that specifies the columns from the table we care about.
String[] projection = {
SupplierEntry._ID,
SupplierEntry.COLUMN_SUPPLIER_NAME};
// This loader will execute the ContentProvider's query method on a background thread
return new CursorLoader(this, // Parent activity context
SupplierEntry.CONTENT_URI, // Provider content URI to query
projection, // Columns to include in the resulting Cursor
null, // No selection clause
null, // No selection arguments
null); // Default sort order
}
How could I, upon selecting an item in the spinner (the name column), show all the other details in some textviews ?
Upvotes: 0
Views: 94
Reputation: 1026
First, set a listener to the spinner so you get a callback when an item is selected.
mSuppliersSpinner.setOnItemSelectedListener(this);
I supply 'this' as listener because my Fragment / Activity implements the interface, but you can write one between the brackets as well. You can implement this method:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
//Start another cursorloader to get the details
}
Based on the id, or the position, you know which entry was selected. At this point you can start another CursorLoader (with a selection so you only get the details of this particular entry). When you get a callback in onLoadFinished, you can show the details in the TextViews.
Upvotes: 1