如何从Android数据库填充Spinner?

时间:2014-02-18 03:41:52

标签: android database spinner populate

我想填充一个微调器下拉列表来显示调用后台线程的列表。我的代码无效。

ArrayAdapter <CharSequence> adapter = new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item );
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapter.add(nameOfMethod(), items);
    itemspin = (Spinner) findViewById(R.id.itemSpin);
    itemspin.setAdapter(adapter);

有人能告诉我我做错了吗?

2 个答案:

答案 0 :(得分:0)

        spinner = (Spinner) findViewById(R.id.spinner);

// database handler
            DatabaseHandler db = new DatabaseHandler(getApplicationContext());

            // Spinner Drop down elements
            List<String> lables = db.getAllLabels();

            // Creating adapter for spinner
            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, lables);

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

答案 1 :(得分:0)

试试这个

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
itemspin.setAdapter(adapter);