Spinner 没有添加来自数据库的所有主题

Spinner not adding all topics from DB

我有一个存储主题的数据库。我想在微调器中显示所有主题。第一个主题能进,其他三个进不去

尽管所有三个日志:

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());

                }

            }

我不知道我在这里遗漏了什么,比如为什么微调器中只显示最后一个列表项?有什么建议吗?

您正在遍历游标并创建一个新的 ArrayList,每次基本上只有一个项目。因此,每次您设置适配器时,它都会得到一件一件的东西。您应该先创建 ArrayList,然后构建适配器:

List<String> myList = new ArrayList<>();
while(cursor.moveToNext()) {
   myList.add(cursor.getString(someIndex));
}
// Build String Array Adapter and set it to Spinner

在循环外创建 labels ArrayList 和 dataAdapter,然后在内部用字符串填充 ArrayList,最后在微调器上使用适配器。

    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);