Spinner没有添加来自DB的所有主题

时间:2015-12-23 17:52:16

标签: android android-spinner android-cursor android-database

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

所有三个登录:

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

                }

            }

我不知道我在这里缺少什么,为什么只有旋转器中显示的最后一个列表项?有什么建议吗?

2 个答案:

答案 0 :(得分:3)

您正在遍历游标并创建一个新的ArrayList,每次只包含一个项目。因此,每次设置适配器时,它都会获得一个项目。您应首先创建ArrayList,然后构建适配器:

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

答案 1 :(得分:2)

在循环外部创建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);