0

我想制作一个内容取决于数据库的微调器,有没有办法做到这一点?早些时候,我读到字符串值不能通过代码更改,所以现在我想知道是否还有其他方法?

4

4 回答 4

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

或者

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

希望能帮助到你。

于 2012-07-23T07:40:55.413 回答
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

于 2012-07-23T07:44:22.570 回答
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));
                }
于 2012-07-23T07:48:05.947 回答
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);        
于 2012-07-23T07:54:22.463 回答