Simple spinner from database

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Simple spinner from database

Postby opy001 » Tue Sep 14, 2010 6:37 pm

I spent countless hours (well a few hours at least lol) researching this and finally figured it out after reading about 50 different ways that didnt work. Basically I wanted to pull a list of items from one table and link them to new items in another. So in table one I manually input data like car, bus, truck (just examples). This list will grow along with the spinner. In table two I manually enter names and want to choose if they drive a car, bus or truck etc. What I do is query table one passing the results to an ArrayList<String>. I then use that arraylist to fill the spinner. When the item is selected it adds the selection to my new form and when I add the data it appends the selected item to the new row in table 2. You wouldnt think this was so hard but for me and several others it was a bit tricky. I have seen alot of people asking about this but none of the solutions worked that well for me. Here is what I did.

I put this in my file. My creates the table and columns and also holds the formats for all my queries to simplify things in activities. It uses to the DBAdapter to create the database itself and have the background code in it (best way i can describe it).
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 // this sets the format for my query I can use multiple times
  2.                 // without haveing to create the loop every time I want to query
  3.         public ArrayList<String> getlist(String table, String[] keys, String selection, String[]
  4.         selectionArgs, String groupBy, String having, String sortBy, String sortOption){
  6.                                 // creates a new arraylist for the results
  7.                 ArrayList<String> list = new ArrayList<String>();
  9.         // calls the DBAdapter for the cursor (again, best way I can describe it)
  10.         Cursor results = database.getAllEntries(table, keys, selection,
  11.             selectionArgs, groupBy, having, sortBy, sortOption);
  13.             // loop to get all rows
  14.                 if ( results!= null)
  15.                 {
  16.                     if (results.moveToFirst())
  17.                     {
  18.                         do
  19.                         {
  20.                             String[] names = results.getColumnNames();
  21.                             int length = names.length;
  22.                             for(int i = length-1; i >= 0; i--) {
  23.                             list.add(results.getString(i));
  24.                         }
  25.                     } while (results.moveToNext()); // move to next row
  26.                 } // if
  27.             } // if
  29.                     // adds the results to the array
  30.             return list;
  31.         }
Parsed in 0.015 seconds, using GeSHi

This is my actual query in the activity to get my wanted data
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         // opens the database
  2.         DatabaseQuery query = new DatabaseQuery(this);
  4.         // specific query to load one column from the table into an array for the spinner
  5.         ArrayList<String> spinnerArray = query.getlist("table", new String[] {"column"}, null,
  6.                         null, null, null, "column", "ASC");
  8.         // closes the database
  9.         try {
  10.                     query.destroy();
  11.             } catch (Throwable e) {
  12.                     e.printStackTrace();
  13.             }
  14.         }
Parsed in 0.011 seconds, using GeSHi

This is how I populate the spinner with the query results
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         // creates new spinner
  2.         Spinner spinner = new Spinner(this);
  4.         // creates new array adapter for the spinner
  5.         // and loads the query results into the simple_spinner_item layout
  6.         ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
  7.                 this, android.R.layout.simple_spinner_item, spinnerArray);
  9.         // loads the selected item into the spinner view in the layout
  10.         spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
  12.         // links the spinner adapter to the spinner (best way I can describe it)
  13.         spinner.setAdapter(spinnerArrayAdapter);
  15.         // shows the spinner in the layout
  16.         spinner = (Spinner) findViewById( );
Parsed in 0.011 seconds, using GeSHi

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                                 // send the selected spinner item to a string to append to database
  2.                 Item = spinner.getSelectedItem().toString();
Parsed in 0.010 seconds, using GeSHi

Once the selected item is in its own string I then append it to be added to the database with the rest of the data I just manually entered.

I tried using the simplecursoradapter and it filled in the spinner fine but once I added it to the database it showed the address of the selected item instead of the item itself. So it showed up as android.database.sqlite.SQLiteCursor@43b8a9b0 instead of "car".

If I screwed up describing any of the steps please let me know and I will edit it. I am not the best at this as you can tell but I try my best :D .

Thanks for reading my tutorial and I hope it helps someone out there.
Experienced Developer
Experienced Developer
Posts: 57
Joined: Sun Jul 11, 2010 5:24 am


Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests