1

I have a database that is storing topics. I want to display all topics in a spinner. I can get the first topic in, but not the other three.

All three log though:

topicCursor: android.database.sqlite.SQLiteCursor@429ba4a8
str: test0
labels: [test0]
dataAdapter: android.widget.ArrayAdapter@429bad58
spinner: android.support.v7.widget.AppCompatSpinner{4296ed00 VFED..C. ......I. 0,0-0,0 #7f0d0055 app:id/spinner}
topicCursor do: android.database.sqlite.SQLiteCursor@429ba4a8
str: test
labels: [test]
dataAdapter: android.widget.ArrayAdapter@429bb6f0
spinner: android.support.v7.widget.AppCompatSpinner{4296ed00 VFED..C. ......I. 0,0-0,0 #7f0d0055 app:id/spinner}
topicCursor do: android.database.sqlite.SQLiteCursor@429ba4a8
str: Testing 
labels: [Testing]
dataAdapter: android.widget.ArrayAdapter@429bbed8
spinner: android.support.v7.widget.AppCompatSpinner{4296ed00 VFED..C. ......I. 0,0-0,0 #7f0d0055 app:id/spinner}
topicCursor do: android.database.sqlite.SQLiteCursor@429ba4a8



public void loadSpinnerData() {
                // database handler
                DatabaseHelper db = DatabaseHelper.getInstance(getApplicationContext());

                Cursor topicCursor = db.getAllTopics();
                Log.v("topicCursor", topicCursor.toString());
                db.close();

                String str;

                if (topicCursor.moveToFirst()) {
                    do {
                        str = topicCursor.getString(topicCursor.getColumnIndex("topic_name"));
                        Log.v("str", str);

                        ArrayList<String> labels = new ArrayList<String>();
                        labels.add(str);
                        Log.v("labels", labels.toString());

                        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, labels);
                        Log.v("dataAdapter", dataAdapter.toString());

                        //Drop down layout style - list view with radio button
                        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                        //Attaching data adapter to spinner
                        spinner.setAdapter(dataAdapter);
                        Log.v("spinner", spinner.toString());
                        Log.v("topicCursor do", topicCursor.toString());
                    } while (topicCursor.moveToNext());

                }

            }

I have no clue what I am missing here like why is only the last list item shown in the spinner? Any suggestions?

JohnWilliams
  • 139
  • 1
  • 13
  • 2
    Note that instead of nesting a do/while inside of an if block, you can simply call `while(cursor.moveToNext())` if the cursor is empty, it won't even move to the first item so it'll just skip over the block all together. It works the same as what you have now but is a lot more readable in my opinion. – AdamMc331 Dec 23 '15 at 18:01

2 Answers2

3

You are iterating through the cursor and creating a new ArrayList with essentially one item each time. So every time you set the adapter it gets one with one item. You should create the ArrayList first and then build an adapter:

List<String> myList = new ArrayList<>();
while(cursor.moveToNext()) {
   myList.add(cursor.getString(someIndex));
}
// Build String Array Adapter and set it to Spinner
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
2

Create your labels ArrayList and dataAdapter outside of the loop, then inside populate the ArrayList with your strings, and finally use the adapter on the spinner.

    ArrayList<String> labels = new ArrayList<String>();
    if (topicCursor.moveToFirst()) {
        do {
            str = topicCursor.getString(topicCursor.getColumnIndex("topic_name"));
            Log.v("str", str);

            labels.add(str);
            Log.v("labels", labels.toString());

        } while (topicCursor.moveToNext());

    }
    Log.v("dataAdapter", dataAdapter.toString());
    Log.v("spinner", spinner.toString());

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, labels);
    //Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    //Attaching data adapter to spinner
    spinner.setAdapter(dataAdapter);
petey
  • 16,914
  • 6
  • 65
  • 97