Sunday, September 11, 2011

Android Programming - Spinner from Database

Spinner is Android UI that looks like dropdown menu in another UI. Like in the other programming language we can directly add Spinner content directly, by hard coding,not dynamic.
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. :)

Categories: , , , , , , ,

Related Posts:

  • Android Programming - Fast Way Preserving Sqlite DbActually this is some snippet I found from browsing, the guy who must be credited to this code is Omar Flores, sorry I can't found your link pal :). It helping me to copy already made SQLite database to my application, it mak… Read More
  • Android Programming - isServerAccessibleThis is my snippet to check wheter web service server accessible or not. Don't forget to edit your Manifest code.. First Code check internet connection available or not, the second check the server accessible or not. . :) pu… Read More
  • Android Programming - Spinner from DatabaseSpinner is Android UI that looks like dropdown menu in another UI. Like in the other programming language we can directly add Spinner content directly, by hard coding,not dynamic. To add spinner content dynamically you need C… Read More
  • Android Programming - Hello WorldOkay, the preparation phase is complete. You can now make Android Project. Let us start with the classic way in which a program Hello World. Let us immediately make, you need not know anything yet, we make first and I will… Read More
  • Android Programming - ListView, ListActivity with HeaderHave been playing around with Android for 2 months maybe, I think ListView very important for me, I used it in my application, and help me build the UI faster. I like to use list adapter and ListActivity, it is simple and gre… Read More

0 komentar:

Copyright © 2025 Johannes Dwi Cahyo | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com | BTheme.net      Up ↑