-1

I have a spinner that I need to fill with information from a database, but the examples that i found don't work.

Here is my code:

public void consulta() {
    AdminSQLiteOpenHelper admin = 
        new AdminSQLiteOpenHelper(this, "administracion", null, 1);
    SQLiteDatabase bd=admin.getWritableDatabase();

    Cursor c = bd.rawQuery("select name  from category",null);

    startManagingCursor(c);

    // create an array to specify which fields we want to display
    String[] from = new String[]{"name"};

    // create an array of the display item we want to bind our data to
    int[] to = new int[]{android.R.id.text1};

    // create simple cursor adapter
    SimpleCursorAdapter adapter =
      new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );

    adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );

    // get reference to our spinner
    spinner1.setAdapter(adapter);

    bd.close();
}
Alex K
  • 22,315
  • 19
  • 108
  • 236
dakairus
  • 43
  • 1
  • 7
  • 1
    Please define "don't work" (post the error log, stack trace, etc...). And welcome to SO! – Jack Apr 26 '12 at 13:48
  • 1
    Where do you define `spinner1`? – Sam Apr 26 '12 at 13:51
  • thanks this is a great web, i find a lot of info, i'm beginer in android. The problem happens when i run the app, the spinner won't fill with anything but if i take that info and cast it in a listview or textview it works, i don' know if the spinner fill method are diferent than other objects – dakairus Apr 26 '12 at 13:54
  • It's pretty much the same as a list. Welcome to SO. Don't forget to mark answers as accepted (click the check mark) and/or upvote them if they help you or solve your issue. – Barak Apr 26 '12 at 14:05

2 Answers2

1

This is how I do it:

db = SQLiteDatabase.openDatabase("/data/data/packagename/databases/mydata.db", null, SQLiteDatabase.OPEN_READONLY);
Cursor c_cat = db.rawQuery("select _id, column_desc from your_table", null);
startManagingCursor(c_cat);
spinner = (Spinner) findViewById(R.id.spinnername);
String[] from = new String[]{"column_desc"};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c_cat, from, to);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(mAdapter);
db.close();

I did all that in my onCreate. I declared all my variables outside the method so that they are public to the whole class. One thing I made a mistake of in the past was closing the cursor after filling the spinner. The cursor must remain open if you want to see data in the spinner.

I hope this solves it for you.

EDIT: I noticed you are not querying for _id in your rawQuery. You must include that if you wish to populate the spinner. Analyze the code I provided.

Nic Raboy
  • 3,143
  • 24
  • 26
  • The code is correct, but I'll encapsulate the DB handling into a class itself (something like a DbAdapter), and in the Activity code, do the call to the DB and populate the spinner. This way the cod eis more flexible and open for future changes. – Rudolf Real Nov 30 '14 at 14:52
0

Assuming you didn't leave any code out, you need to find/define spinner1, so that the framework knows where to put your info from the adapter.

private Spinner spinner1;

spinner1 = (Spinner) view.findViewById(R.id.yourspinnerid);

// rest of your code
Barak
  • 16,318
  • 9
  • 52
  • 84
  • i put the spinner define in the oncreate class – dakairus Apr 26 '12 at 14:06
  • @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et1=(EditText)findViewById(R.id.editText1); spinner1=(Spinner)findViewById(R.id.spinner1); consulta(); } – dakairus Apr 26 '12 at 14:07
  • Do you declare it there as well as set the value, or do you declare it so it's available to the whole class? It sounds like the framework can't find it to load the adapter into it. You could try making it public, or move it so it's declared in the main class rather than in onCreate. – Barak Apr 26 '12 at 14:10
  • Ah, looks like you declare it for the whole thing... I'm now officially at a loss as to why it's not working. – Barak Apr 26 '12 at 14:12
  • public class Proyecto015Activity extends Activity { private EditText et1; private Spinner spinner1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et1=(EditText)findViewById(R.id.editText1); spinner1=(Spinner)findViewById(R.id.spinner1); consulta(); } public void consulta(){} – dakairus Apr 26 '12 at 14:17