动态微调器

时间:2012-07-23 07:23:23

标签: android spinner

我想创建一个内容依赖于数据库的微调器,有没有办法做到这一点?之前,我读过字符串值无法通过代码更改,所以现在我想知道是否还有其他方法?

4 个答案:

答案 0 :(得分:2)

创建Adapter

spinner = (Spinner)this.findViewById(R.id.spr);
adapter = ArrayAdapter.createFromResource(
        this, R.array.some_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

OR

将它绑定在Listener,每次有人与微调器交互时,检查数据库中是否有变化。

希望它有所帮助。

答案 1 :(得分:1)

您可以使用Spinner来管理Adapter的内容。

ArrayAdapter的示例:

Spinner spinner = new Spinner(this);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

// Then add all the values you want...
adapter.add("Item 1");
adapter.add("Item 2");

在此处查找文档:http://developer.android.com/guide/topics/ui/controls/spinner.html

答案 2 :(得分:1)

/Bind values to spinner through an array
String[] items = new String[] {"Karnataka", "Orissa", "Andhrapradesh"};
Spinner spinner;

spinner = (Spinner)this.findViewById(R.id.spnState);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

//Bind values to spinner through databases

//--Write a query to get all the values in a column in a table
public Cursor columnValues() throws SQLException {
SQLiteDatabase db;
        return db.query(TABLE_NAME, new String[] { ID, KEY_NAME }, null, null, null, null, KEY_NAME + " ASC", null);
    }
//--then in activity under button click write,
    Cursor cursor = db.columnValues();
        startManagingCursor(cursor);

        // create an array to specify which fields we want to display
        String[] from = new String[] { DBAdapter.KEY_NAME };
        // create an array of the display item we want to bind our data to
        int[] to = new int[] { android.R.id.text1 };

        Spinner spinner = (Spinner) this.findViewById(R.id.spinnerID);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_spinner_item, cursor, from, to);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

//How to get selected values from spinner

                Spinner spinner = (Spinner) findViewById(R.id.spinnerID);
                String selectedItem;
                Cursor cc = (Cursor)(spinner.getSelectedItem());
                if (cc != null) {
                    selectedItem = cc.getString(cc.getColumnIndex(db.KEY_NAME));
                }

答案 3 :(得分:0)

我发现ORMLite在处理Android中的数据库时非常有用。你可以在你的代码中使用这样的东西:

Dao<YourTable, YourTableKeyType> tableDao = DaoManager.createDao(connectionSource,
                    YourTable.class);

List<YourTable> itemsList = tableDao.queryForAll();

tableStr = new String[tableList.size()];
int i = 0;
for (YourTable item : itemsList) {
    tableStr[i++] = item.getNom();
}
spinner = (Spinner) findViewById(R.id.spinner_id);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    R.layout.spinner_layout, tableStr);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner.setAdapter(adapter);