2016-03-14 73 views
2

我想將SQLite數據庫文件連接到一個選擇器組件(接受字符串)。這應該類似於下拉菜單。我試圖按照以前的建議和例子,但沒有成功。Codename One:連接並填充SQLite數據庫的下拉菜單

如前一篇文章所述,我已將數據庫文件保存在應用程序的源文件夾中。 View of the source folder where I have saved the database file (highlighted).

我用來實現我的應用程序的代碼如下面的佈局。

// -----------------------

數據庫代碼

// --------- --------------

public class MyApplication { 

private Form current; 
private Resources theme; 

public void init(Object context) { 
    theme = UIManager.initFirstTheme("/theme"); 

    // Pro only feature, uncomment if you have a pro subscription 
    // Log.bindCrashProtection(true); 
} 

private Container Home() { 
    Container home = new Container(new BoxLayout(BoxLayout.Y_AXIS)); 
    return home; 
} 

private Container AddItem() { 
    Container addItem = new Container(new BoxLayout(BoxLayout.Y_AXIS)); 

    TextArea item = new TextArea("Add Item"); 
    addItem.addComponent(item); 
    Picker selectItem = new Picker(); 
    selectItem.setType(Display.PICKER_TYPE_STRINGS); 

//----------------------------------------------------------------------------------   

Database db = null; 
Cursor cur = null; 
try { 
    db = Display.getInstance().openOrCreate("FoodAndBeverage.db"); 
    if(selectItem.getText().startsWith("Still Water")) { 
     cur = db.executeQuery(selectItem.getText()); 
     int columns = cur.getColumnCount(); 
     addItem.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); 
       addItem.add(BorderLayout.CENTER, new Table(new DefaultTableModel(columnNames, arr))); 
      } else { 
       addItem.add(BorderLayout.CENTER, "Query returned no results"); 
      } 
     } else { 
      addItem.add(BorderLayout.CENTER, "Query returned no results"); 
     } 
    } else { 
     db.execute(selectItem.getText()); 
     addItem.add(BorderLayout.CENTER, "Query completed successfully"); 
    } 
    addItem.revalidate(); 
} catch(IOException err) { 
    Log.e(err); 
    addItem.removeAll(); 
    addItem.add(BorderLayout.CENTER, "Error: " + err); 
    addItem.revalidate(); 
} finally { 
    Util.cleanup(db); 
    Util.cleanup(cur); 
} 

//--------------------------------------------------------------------------------------------- 

    addItem.addComponent(selectItem); 
    TextField quantity = new TextField("", "Quantity (ml or g)", 4, TextArea.NUMERIC); 
    addItem.addComponent(quantity); 
    Button add = new Button("Add"); 
    addItem.addComponent(add); 
    TextArea results = new TextArea("Results"); 
    addItem.addComponent(results); 
    return addItem; 
} 

private Container Settings() { 
    Container settings = new Container(new BoxLayout(BoxLayout.Y_AXIS));  
    TextArea nutrients = new TextArea("Target"); 
    settings.addComponent(nutrients); 
    TextField volume = new TextField("", "Volume (ml)", 4, TextArea.NUMERIC); 
    settings.addComponent(volume); 
    TextArea duration = new TextArea("Hydration Duration"); 
    settings.addComponent(duration); 
    settings.add("Start:"); 
    Picker start = new Picker(); 
    start.setType(Display.PICKER_TYPE_TIME); 
    settings.addComponent(start); 
    settings.add("End:"); 
    Picker end = new Picker(); 
    end.setType(Display.PICKER_TYPE_TIME); 
    settings.addComponent(end);  
    Button save = new Button("Save"); 
    settings.addComponent(save); 
    return settings; 
} 

public void start() { 
    if(current != null) 
    { 
     current.show(); 
     return; 
    } 

    Form home = new Form("Hydrate", new BorderLayout()); 
    Tabs t = new Tabs(); 
    t.addTab("Home", Home()); 
    t.addTab("Intake", AddItem()); 
    t.addTab("Settings", Settings()); 
    home.add(BorderLayout.NORTH, t); 
    home.show(); 

} 

public void stop() { 
    current = Display.getInstance().getCurrent(); 
} 

public void destroy() { 
} 

} 

因此,我將不勝感激正是我錯了,如何實現我的代碼修改建議的任何意見和指導。

+0

我用'String ... getDatabasePath(「FoodAndBeverage.db/Items」)'而不是'openOrCreate()'方法。這排序我的SQLite連接問題(我認爲),但仍然不填充選擇器。這可能是由於if(selectItem.getText()。startsWith(「Still Water」)),因爲它引用了選取器組件的名稱。因此,我想感謝任何有關如何在我的代碼中實施這些建議和指導的建議和指導。 –

回答

0

我假設src下的文件確實以擴展db結尾,因爲Windows隱藏的擴展無意義已打開。

此代碼將打開文件放在src分貝:

db = Display.getInstance().openOrCreate("FoodAndBeverage.db"); 

你需要做的是這樣的隱式初始化DB首次安裝該應用程式:

String path = Display.getInstance().getDatabasePath("FoodAndBeverage.db"); 
FileSystemStorage fs = FileSystemStorage.getInstance(); 
if(!fs.exists(path)) { 
    try (InputStream is = Display.getInstance().getResourceAsStream(getClass(), "/FoodAndBeverage.db"); 
      OutputStream os = fs.openOutputStream(path)) { 
     Util.copy(is, os); 
    } catch(IOException err) { 
      Log.e(err); 
    } 
} 
db = Display.getInstance().openOrCreate("FoodAndBeverage.db"); 

請注意,上面的代碼不檢查數據庫的更新,因此假設數據庫是隻讀的,您可能想要使用應用程序更新來更新/合併數據庫。

0

上面的代碼不適用於Android設備,這隻適用於模擬器。我已經測試了多次在Android設備。在真正的Android設備,數據庫完全不加載,顯示SQL異常錯誤

「沒有這樣的表SQL異常」。

貌似.db的文件從來沒有真實的Android設備測試預裝sqlite的。

相關問題