Codename One:如何将SQLite连接到选择器组件并将字符串添加到时间选择器组件

时间:2016-03-09 12:38:19

标签: codenameone

  1. 如何使用外部SQLite数据库中的项目填充选择器(接受字符串)? View of picker (accepting strings) to be populated
  2. 如何将字符串添加到时间选择器以指示用户需要选择的时间?用户需要选择两次(开始和结束),我想向用户表明他们需要为每个时间选择器选择哪一个View of the two times to be chosen by the user

1 个答案:

答案 0 :(得分:0)

您可以使用:

填充字符串Picker
stringPicker.setStrings("A Game of Thrones", "A Clash Of Kings", "A Storm Of Swords", "A Feast For Crows",
        "A Dance With Dragons", "The Winds of Winter", "A Dream of Spring");
stringPicker.setSelectedString("A Game of Thrones");

此代码来自Picker JavaDocs

您可以通过涵盖here (JavaDoc)&的db API查询sqlite。 here (Developer Guide)

    Database db = null;
    Cursor cur = null;
    try {
        db = Display.getInstance().openOrCreate("MyDB.db");
        if(query.getText().startsWith("select")) {
            cur = db.executeQuery(query.getText());
            int columns = cur.getColumnCount();
            hi.removeAll();
            if(columns > 0) {
                boolean next = cur.next();
                if(next) {
                    ArrayList<String[]> data = new ArrayList<>();
                    String[] columnNames = new String[columns];
                    for(int iter = 0 ; iter < columns ; iter++) {
                        columnNames[iter] = cur.getColumnName(iter);
                    }
                    while(next) {
                        Row currentRow = cur.getRow();
                        String[] currentRowArray = new String[columns];
                        for(int iter = 0 ; iter < columns ; iter++) {
                            currentRowArray[iter] = currentRow.getString(iter);
                        }
                        data.add(currentRowArray);
                        next = cur.next();
                    }
                    Object[][] arr = new Object[data.size()][];
                    data.toArray(arr);
                    hi.add(BorderLayout.CENTER, new Table(new DefaultTableModel(columnNames, arr)));
                } else {
                    hi.add(BorderLayout.CENTER, "Query returned no results");
                }
            } else {
                hi.add(BorderLayout.CENTER, "Query returned no results");
            }
        } else {
            db.execute(query.getText());
            hi.add(BorderLayout.CENTER, "Query completed successfully");
        }
        hi.revalidate();
    } catch(IOException err) {
        Log.e(err);
        hi.removeAll();
        hi.add(BorderLayout.CENTER, "Error: " + err);
        hi.revalidate();
    } finally {
        Util.cleanup(db);
        Util.cleanup(cur);
    }

您无法更改时间选择器的外观,因为它在Android上会如下所示:

Time picker for Android

但是,如果您在谈论显示实际启动前一小时的用户界面,则可以通过覆盖updateValue来自定义。