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 - Setting Up Your EnvironmentThis is my first article of Android Programming Series in my blog. I'll share you how to set up an environment to begin programming in Android. Actually it was easy and the Android Developer site have a full article about thi… Read More
  • Android Programming - Url ConnectThis the basis for web service. Android + Web Service is a future will be for me. With open platform you can access open information. That's really great I think. Simple, with a piece of code to access web service in Android,… Read More
  • Otak-atik Ubuntu 11.04Berawal dari setting VGA SiS itu, saya jadi terarik otak-atik si Ubuntu, sesuai dengan niatan juga untuk pindah framework pemrograman Eclipse saya ke Ubuntu, biar lebih terasa lagi opensourcenya.Karena tujuan awal memindahkan… Read More
  • Installing Metapsploit 4 in Ubuntu 11.04 I think you've already know what is Metasploit if you come up to my blog post from search engine. If not you can read here what is metasploit, what is need for, what capabilities of, etc.. Installing Metasploit is easy, j… Read More
  • 10 Essential UNIX/Linux Command Cheat Sheets Linux has become so idiot proof nowadays that there is less and less need to use the command line. However, the commands and shell scripts have remained powerful for advanced users to utilize to help them do complicated t… Read More

0 komentar:

Copyright © 2025 Johannes Dwi Cahyo | Powered by Blogger

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