0
        db.open();
        cursor = db.getAllRecords();
        int i = 0;
        for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()) { 
            spinnerItems[i] = cursor.getString(NAME_COLUMN);
            Log.v(TAG, "spinnerItem "+i+"="+spinnerItems[i]);
            i++;
        }

Why does the code above give this result in LogCat?:

spinnerItem 0=name1
spinnerItem 1=name2
spinnerItem 2=name3
spinnerItem 3=name4 and so on

When the database looks like this:

row name
0   name0
1   name1
2   name2
3   name3
4   name4 and so on.

And what is the best way to populate a spinner with the entire content from one column of a database? Above I (try to) create spinnerItems[] whish I then arrayAadapters into the spinner.

Tombola
  • 1,131
  • 5
  • 15
  • 26

1 Answers1

1

For you main question, the reason everything is off is in your for loop logic.

The loop starts with moveToFirst(), - cursor now on row 0. Then, the loop checks out if it passes the conditional, which is a call to moveToNext() - cursor now on row 1.

So the very first iteration, your cursor is on row 1 (or it will bypass the loop entirely if your cursor only has 1 row!).

A better for loop:

int i=0;
for (cursor.moveToFirst(); i < cursor.getCount(); i++) {
//Your code
cursor.moveToNext();
}

In regards to your other question, unfortunately I have no answer... I always build them programmaticaly like that as well. A quick google search provided me with this, though: http://androidforbeginners.blogspot.com/2010/02/how-to-populate-spinner-widget-from.html

Geekswordsman
  • 1,297
  • 8
  • 10
  • A little more digging, and your second question was answered - http://stackoverflow.com/questions/2196426/populating-spinner-from-sqlite-database-android Pretty much, use a SimpleCursorAdapter. – Geekswordsman Feb 16 '12 at 21:57