Spinner with data from the database problem.

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Spinner with data from the database problem.

Postby method1545 » Mon Oct 19, 2009 1:35 pm

Hi,

i want to fill a spinner with data from the database.

Like showing all persons in a spinner to give the user the possibility to choose one.
If the user adds a person for example, the spinner should show the new person, too.

I am not getting it going with the ArrayAdapter.
method1545
Freshman
Freshman
 
Posts: 5
Joined: Mon Oct 19, 2009 1:24 pm

Top

Postby qlimax » Mon Oct 19, 2009 2:31 pm

can you give us more details please ?...

i think in general,
when you add a row to your db table, then you have to call an updateSpinner function that:

-make a query to the db table
-store the result values in a array
-construct a new ArrayAdapter
-call the setAdapter on the spinner object to set up the new arrayadapter
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby method1545 » Mon Oct 19, 2009 2:39 pm

my code looks like this but the spinner is empty.
can't see the problem.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. private void fillSpinner()
  3.  
  4.         {
  5.  
  6.  
  7.  
  8.                 Cursor spinnerCursor = mDbHelper.fetchAllPersons();
  9.  
  10.  
  11.  
  12.                 if (spinnerCursor != null)
  13.  
  14.                 {
  15.  
  16.                         startManagingCursor(spinnerCursor);
  17.  
  18.  
  19.  
  20.  
  21.  
  22.                         String[] names = new String[]
  23.  
  24.                         { PersonDbAdapter.KEY_FIRSTNAME };
  25.  
  26.  
  27.  
  28.                         int[] to = new int[]
  29.  
  30.                         { android.R.layout.simple_spinner_item };
  31.  
  32.  
  33.  
  34.                        
  35.  
  36.                         SimpleCursorAdapter itemsAdapter = new SimpleCursorAdapter(this,
  37.  
  38.                                         android.R.layout.simple_spinner_item, spinnerCursor, names,
  39.  
  40.                                         to);
  41.  
  42.  
  43.  
  44.                         // itemsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  45.  
  46.                         mPersonSpinner.setAdapter(itemsAdapter);
  47.  
  48.                         // itemsAdapter.bindView(mDriverSpinner, this, spinnerCursor);
  49.  
  50.                 } else
  51.  
  52.                 {
  53.  
  54.                         Toast.makeText(this, "error: items cursor is null",
  55.  
  56.                                         Toast.LENGTH_SHORT);
  57.  
  58.                 }
  59.  
  60.         }
  61.  
  62.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
method1545
Freshman
Freshman
 
Posts: 5
Joined: Mon Oct 19, 2009 1:24 pm

Postby qlimax » Mon Oct 19, 2009 7:29 pm

hi :)

sorry today I was not at home...

I've tried your case. i simulated an example.....
I've to say that it's not completly clear from the sdk doc.

anyway, look at this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7.  
  8. <Spinner
  9. android:id="@+id/MySpinner"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"></Spinner>
  12. </LinearLayout>
Parsed in 0.007 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package ch.example.db;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.widget.SimpleCursorAdapter;
  6. import android.widget.Spinner;
  7.  
  8. public class DbSpinner extends Activity {
  9.     /** Called when the activity is first created. */
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.        
  15.         DbUtility db=new DbUtility(this);
  16.        
  17.         SimpleCursorAdapter adapter=db.getTableAdapter();
  18.        
  19.         Spinner spinner=(Spinner)this.findViewById(R.id.MySpinner);
  20.        
  21.         spinner.setAdapter(adapter);
  22.        
  23.     }
  24. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package ch.example.db;
  2. import android.content.Context;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteOpenHelper;
  6. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  7. import android.widget.SimpleCursorAdapter;
  8.  
  9.  
  10.  
  11.  
  12. public class DbUtility {
  13.  
  14.         static final String DB_NAME="contacts";
  15.         static final String NAMES_TABLE="names";
  16.        
  17.         SQLiteDatabase db=null;
  18.        
  19.         Context context;
  20.        
  21.         public static class DatabaseHelper extends SQLiteOpenHelper{
  22.  
  23.                 public DatabaseHelper(Context context, String name,
  24.                                 CursorFactory factory, int version) {
  25.                         super(context, name, factory, version);
  26.                         // TODO Auto-generated constructor stub
  27.                 }
  28.  
  29.                 @Override
  30.                 public void onCreate(SQLiteDatabase db) {                      
  31.                         db.execSQL("CREATE TABLE IF NOT EXISTS "
  32.                                         +NAMES_TABLE
  33.                                         +" (_id INT PRIMARY KEY ,name VARCHAR);"
  34.                         );
  35.                        
  36.                         db.execSQL("INSERT INTO "+NAMES_TABLE+" values(1,'marco');");
  37.                         db.execSQL("INSERT INTO "+NAMES_TABLE+" values(2,'luca');");
  38.                         db.execSQL("INSERT INTO "+NAMES_TABLE+" values(3,'qlimax');");
  39.                        
  40.                 }
  41.  
  42.                 @Override
  43.                 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  44.                        
  45.                        
  46.                 }
  47.                
  48.         }
  49.        
  50.        
  51.         public DbUtility(Context context) {
  52.                
  53.                 this.context=context;
  54.                
  55.        
  56.  
  57.         }
  58.        
  59.         public SimpleCursorAdapter getTableAdapter(){
  60.                 DatabaseHelper dbhelper=new DatabaseHelper(this.context, DB_NAME, null, 2);
  61.                 db=dbhelper.getWritableDatabase();
  62.  
  63.                 Cursor c=db.rawQuery("SELECT * FROM "+NAMES_TABLE, null);
  64.                
  65.        
  66.                
  67.                 while(!c.isLast())
  68.                         c.moveToNext(); //I dont understand why but it's necessary (alternative call c.getCount() )
  69.                
  70.                 String[] from=new String[1];
  71.                 from[0]="name";
  72.                
  73.                 int[] to=new int[1];
  74.                 to[0]=android.R.id.text1;
  75.                
  76.                
  77.                 SimpleCursorAdapter adapter=new SimpleCursorAdapter(this.context, android.R.layout.simple_spinner_item, c, from, to);
  78.                
  79.                
  80.                
  81.                 db.close();
  82.                
  83.                 return adapter;
  84.                
  85.         }
  86.        
  87.        
  88.        
  89. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4



Image




CYA :run:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Re: Spinner with data from the database problem.

Postby strider2023 » Thu Dec 02, 2010 1:37 pm

Hey qlimax,

thanx for this nice tutorial...but can u tell me how we can get the row id after the selection. I want to get the row id so that i can display other additional info which otherwise is not displayed in the spinner.
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Re: Spinner with data from the database problem.

Postby SickNick » Sun Dec 05, 2010 4:46 pm

How do you pull the data from the extra values from the spinner?
Last edited by SickNick on Mon Dec 06, 2010 10:39 pm, edited 1 time in total.
SickNick
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Oct 23, 2010 4:52 pm

Top

Re: Spinner with data from the database problem.

Postby strider2023 » Mon Dec 06, 2010 11:11 am

Never mind solved it... :mrgreen:
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Re: Spinner with data from the database problem.

Postby SickNick » Tue Dec 07, 2010 12:01 am

I am trying to pull data from the spinners other columns and pass them to an activity though an intent. My problem is trying to understand how to grab the other columns.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DbUtility {
  2.  
  3.         static final String DB_NAME="food";
  4.         static final String BEEF_TABLE="beef";
  5.         static final String CHICKEN_TABLE="chicken";
  6.  
  7.         SQLiteDatabase db=null;
  8.         Context context;
  9.        
  10.         public static class DatabaseHelper extends SQLiteOpenHelper
  11.         {
  12.                 public DatabaseHelper(Context context, String name, CursorFactory factory, int version)
  13.                 {
  14.                         super(context, name, factory, version);
  15.                         // TODO Auto-generated constructor stub
  16.                 }
  17.                 @Override
  18.                 public void onCreate(SQLiteDatabase db)
  19.                 {                      
  20. db.execSQL("CREATE TABLE IF NOT EXISTS "+BEEF_TABLE+" (_id INT PRIMARY KEY, cookTime INT,name VARCHAR);");
  21. db.execSQL("INSERT INTO "+BEEF_TABLE+" values(1,5000,'Skirt Steak');");
  22. db.execSQL("INSERT INTO "+BEEF_TABLE+" values(2,10000,'Flank Steak');");
  23. db.execSQL("INSERT INTO "+BEEF_TABLE+" values(3,15000,'Filet Mignon');");
  24. db.execSQL("CREATE TABLE IF NOT EXISTS "+CHICKEN_TABLE+" (_id INT PRIMARY KEY, cookTime INT,name VARCHAR);");
  25. db.execSQL("INSERT INTO "+CHICKEN_TABLE+" values(1,5000,'Breast');");
  26. db.execSQL("INSERT INTO "+CHICKEN_TABLE+" values(2,10000,'Wings');");
  27. db.execSQL("INSERT INTO "+CHICKEN_TABLE+" values(3,15000,'Burger');");
  28.                 }
  29.                 @Override
  30.                 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {     
  31.                 }      
  32.         }
  33.         public DbUtility(Context context) {
  34.                 this.context=context;
  35.         }
  36.         public SimpleCursorAdapter getBeefAdapter()
  37.         {
  38.                 DatabaseHelper dbhelper=new DatabaseHelper(this.context, DB_NAME, null, 1);
  39.                 db=dbhelper.getWritableDatabase();
  40.  
  41.                 Cursor beefCursor=db.rawQuery("SELECT * FROM "+BEEF_TABLE, null);
  42.                 while(!beefCursor.isLast())
  43.                 beefCursor.moveToNext(); //I don't understand why but it's necessary (alternative call c.getCount() )
  44.                 String[] from=new String[1];
  45.                 from[0]="name";
  46.                 int[] to=new int[1];
  47.                 to[0]=android.R.id.text1;
  48.                 SimpleCursorAdapter beefAdapter=new SimpleCursorAdapter(this.context, android.R.layout.simple_spinner_item, beefCursor, from, to);
  49.                 beefAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  50.                 db.close();
  51.                 return beefAdapter;    
  52.         }      
  53.         public SimpleCursorAdapter getChickenAdapter()
  54.         {
  55.                 DatabaseHelper dbhelper=new DatabaseHelper(this.context, DB_NAME, null, 1);
  56.                 db=dbhelper.getWritableDatabase();
  57.  
  58.                 Cursor chickenCursor=db.rawQuery("SELECT * FROM "+CHICKEN_TABLE, null);
  59.                 while(!chickenCursor.isLast())
  60.                 chickenCursor.moveToNext(); //I don't understand why but it's necessary (alternative call c.getCount() )
  61.                 String[] from=new String[1];
  62.                 from[0]="name";
  63.                 int[] to=new int[1];
  64.                 to[0]=android.R.id.text1;
  65.                 SimpleCursorAdapter chickenAdapter=new SimpleCursorAdapter(this.context, android.R.layout.simple_spinner_item, chickenCursor, from, to);
  66.                 chickenAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  67.                 db.close();
  68.                 return chickenAdapter; 
  69.         }      
  70. }
Parsed in 0.047 seconds, using GeSHi 1.0.8.4


I need to receive cookTime INT in this activity:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ChickenActivity extends Activity {
  2.  
  3.         int option1value;
  4.         int option2value;
  5.        
  6.         /** Called when the activity is first created. */
  7.         @Override
  8.        
  9.     public void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.spinner);
  12.        
  13.         DbUtility db=new DbUtility(this);
  14.        
  15.         SimpleCursorAdapter beefAdapter=db.getBeefAdapter();
  16.         Spinner beefSpinner=(Spinner)this.findViewById(R.id.spinner);
  17.         beefSpinner.setAdapter(beefAdapter);
  18.        
  19.         SimpleCursorAdapter chickenAdapter=db.getChickenAdapter();
  20.         Spinner chickenSpinner=(Spinner)this.findViewById(R.id.spinner2);
  21.         chickenSpinner.setAdapter(chickenAdapter);        
  22.        
  23.         Button TimerButton = (Button)findViewById(R.id.TimerActivityButton);
  24.         TimerButton.setOnClickListener(new View.OnClickListener(){
  25.                 public void onClick(View v)
  26.                 {
  27.                         Intent timer = new Intent (ChickenActivity.this,TimerActivity.class);
  28.                         timer.putExtra("option1", option1value);
  29.                         timer.putExtra("option2", option2value);
  30.                         startActivity(timer);
  31.                 }
  32.         });
  33.         }
  34. }
  35.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


How do I do this?
SickNick
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Oct 23, 2010 4:52 pm

Re: Spinner with data from the database problem.

Postby SickNick » Wed Dec 08, 2010 10:03 pm

Nobody?
SickNick
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Oct 23, 2010 4:52 pm

Re: Spinner with data from the database problem.

Postby qlimax » Thu Dec 09, 2010 9:07 am

To retrive the other column's values, you can query the db when you need it.
You can set an itemSelectedListener on the Spinner, then in the listener you perform a query on the db like:
select cookTime from chicken where name='selectedSpinnerValue'
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Re: Spinner with data from the database problem.

Postby jmbanuelosRobles » Thu Feb 24, 2011 10:16 am

qlimax wrote:hi :)

sorry today I was not at home...

I've tried your case. i simulated an example.....
I've to say that it's not completly clear from the sdk doc.

anyway, look at this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7.  
  8. <Spinner
  9. android:id="@+id/MySpinner"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"></Spinner>
  12. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package ch.example.db;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.widget.SimpleCursorAdapter;
  6. import android.widget.Spinner;
  7.  
  8. public class DbSpinner extends Activity {
  9.     /** Called when the activity is first created. */
  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.        
  15.         DbUtility db=new DbUtility(this);
  16.        
  17.         SimpleCursorAdapter adapter=db.getTableAdapter();
  18.        
  19.         Spinner spinner=(Spinner)this.findViewById(R.id.MySpinner);
  20.        
  21.         spinner.setAdapter(adapter);
  22.        
  23.     }
  24. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package ch.example.db;
  2. import android.content.Context;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.database.sqlite.SQLiteOpenHelper;
  6. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  7. import android.widget.SimpleCursorAdapter;
  8.  
  9.  
  10.  
  11.  
  12. public class DbUtility {
  13.  
  14.         static final String DB_NAME="contacts";
  15.         static final String NAMES_TABLE="names";
  16.        
  17.         SQLiteDatabase db=null;
  18.        
  19.         Context context;
  20.        
  21.         public static class DatabaseHelper extends SQLiteOpenHelper{
  22.  
  23.                 public DatabaseHelper(Context context, String name,
  24.                                 CursorFactory factory, int version) {
  25.                         super(context, name, factory, version);
  26.                         // TODO Auto-generated constructor stub
  27.                 }
  28.  
  29.                 @Override
  30.                 public void onCreate(SQLiteDatabase db) {                      
  31.                         db.execSQL("CREATE TABLE IF NOT EXISTS "
  32.                                         +NAMES_TABLE
  33.                                         +" (_id INT PRIMARY KEY ,name VARCHAR);"
  34.                         );
  35.                        
  36.                         db.execSQL("INSERT INTO "+NAMES_TABLE+" values(1,'marco');");
  37.                         db.execSQL("INSERT INTO "+NAMES_TABLE+" values(2,'luca');");
  38.                         db.execSQL("INSERT INTO "+NAMES_TABLE+" values(3,'qlimax');");
  39.                        
  40.                 }
  41.  
  42.                 @Override
  43.                 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  44.                        
  45.                        
  46.                 }
  47.                
  48.         }
  49.        
  50.        
  51.         public DbUtility(Context context) {
  52.                
  53.                 this.context=context;
  54.                
  55.        
  56.  
  57.         }
  58.        
  59.         public SimpleCursorAdapter getTableAdapter(){
  60.                 DatabaseHelper dbhelper=new DatabaseHelper(this.context, DB_NAME, null, 2);
  61.                 db=dbhelper.getWritableDatabase();
  62.  
  63.                 Cursor c=db.rawQuery("SELECT * FROM "+NAMES_TABLE, null);
  64.                
  65.        
  66.                
  67.                 while(!c.isLast())
  68.                         c.moveToNext(); //I dont understand why but it's necessary (alternative call c.getCount() )
  69.                
  70.                 String[] from=new String[1];
  71.                 from[0]="name";
  72.                
  73.                 int[] to=new int[1];
  74.                 to[0]=android.R.id.text1;
  75.                
  76.                
  77.                 SimpleCursorAdapter adapter=new SimpleCursorAdapter(this.context, android.R.layout.simple_spinner_item, c, from, to);
  78.                
  79.                
  80.                
  81.                 db.close();
  82.                
  83.                 return adapter;
  84.                
  85.         }
  86.        
  87.        
  88.        
  89. }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4



Image




CYA :run:

Thank you, for these examples, I am studying them to know as loading the data of my database, I am a beginner in android, although I have programmed in several languages, and several database, as much java as android, they are new for my.

I am creating a commercial application, I much still lack for it achieves a better result.

J. Mamuel Banuelos.
jmbanuelosRobles
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Feb 23, 2011 4:17 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 10 guests