1

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

虽然所有三个日志:

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

                }

            }

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

4

2 回答 2

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
于 2015-12-23T17:59:20.607 回答
2

创建您的labelsArrayList 并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);
于 2015-12-23T17:59:43.003 回答