To add spinner content dynamically you need CursorAdapter. I'll make my spinner load content dynamically with SimpleCursorAdapter.
Cursor cClass = dbHelper.fetchAllClass(); startManagingCursor(cClass); SimpleCursorAdapter scaClass = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cClass, new String[] {GSDbAdapter.KEY_CLASS}, new int[]{android.R.id.text1}); scaClass.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); classSpinner = (Spinner) findViewById(R.id.id_class); classSpinner.setAdapter(scaClass);
Cursor cClass come from database query fetchAllClass, then I create SimpleCursorAdapter named scaClass. This adapter will use template from default android layout simple_spinner_item, containing value from KEY_CLASS column in database that will be placed to TextView with id text1.
Then I set the drop drown view resource using layout simple_spinner_dropdown and finally find the spinner Id and add the SimpleCursorAdapter to it.
Note : to make this code work you need database helper class and tempate for your activity. :)
0 komentar:
Post a Comment