2011-12-20 37 views
0

我使用了一個靜態列表並檢查了所選的值,它是正確的。現在將相同的代碼應用到從SQLite數據庫動態填充的列表中,顯示的是所點擊值的垃圾值。下面是代碼檢索從列表字段中選擇的值

class mal2eng extends MainScreen 
          implements ListFieldCallback { 


    private ListField _listfield; 


    private Item _selected = null; 



    private Vector _listVector = new Vector(); 



    mal2eng() { 

     Database d; 
     try 
       { 
        URI myURI=URI.create("file:///SDCard/Databases/MyTestDatabase.db"); 
        d=DatabaseFactory.open(myURI); 
        Statement st=d.createStatement("SELECT malLetter FROM MalayalamLetter"); 
        st.prepare(); 
        net.rim.device.api.database.Cursor c=st.getCursor(); 
        Row r; 

        while(c.next()) 
        { 

        r=c.getRow(); 
        String w=r.getString(0); 

        _listVector.addElement(new Item(w)); 
        } 
        st.close(); 
        d.close(); 
        } 
        catch (Exception e) 
     {   
      System.out.println(e.getMessage()); 
      e.printStackTrace(); 
     }      


     this.setTitle("List"); 


     _listfield = new ListField(_listVector.size()); 
     _listfield.setCallback(this); 
      FontManager.getInstance().load("DC124.TTF", "MyFont", FontManager.APPLICATION_FONT) ; 
     { 
      try { 
       FontFamily typeface = FontFamily.forName("MyFont"); 
       Font myFont = typeface.getFont(Font.PLAIN, 25); 
       _listfield.setFont(myFont); 
       this.add(_listfield); 
      } catch (ClassNotFoundException ex) { 
       ex.printStackTrace();} 

      } 



    } 

    protected boolean onSavePrompt() { 
     return true; 
    } 

    protected void makeMenu(Menu menu, int instance) { 

    } 

    public void drawListRow(ListField list, Graphics g, int index, int y, int w) { 
     Item ToDraw = (Item) this.get(list, index); 
     int drawColor = Color.BLACK; 

     g.setColor(drawColor); 
     g.drawText(ToDraw.word, 0, y, 0, w); 
    } 


    public Object get(ListField list, int index) { 

     return _listVector.elementAt(index); 
    } 

    public int getPreferredWidth(ListField list) { 
     return Display.getWidth(); 
    } 

    public int indexOfList(ListField listField, String prefix, int start) { 

     return start; 
    } 

    public boolean navigationClick(int status, int time) { 
    Field focus = _listfield.getLeafFieldWithFocus(); 


    if (focus instanceof ListField) { 
     ListField listField = (ListField)focus; 
     String w=listField.getCallback().get(listField,listField.getSelectedIndex()).toString(); 
     UiApplication ui = UiApplication.getUiApplication(); 
     ui.pushScreen(new ListFieldScreen(w)); 
    } 
    return true; 
} 
} 

回答

0

因爲你是從數據庫中獲取數據使用像Vector或那樣的東西數據結構,你可以做一個小的事情。將數據放入您從數據庫中獲取的向量中。 覆蓋navigationClick method像下面...

navigationClickMethod(int, int){ 
//Call getSelectedindex here 

// And use this to call Element at the selected index of the dataStructure you have 

} 

這是獲得從列表中選擇的值最簡單的方法...

+0

我使用的是載體,也是導航點擊方法,我已經發布在上面的代碼中,最後一個對話框顯示了之前在靜態列表中選擇的值,但現在不是。其他方式? – j2me 2011-12-20 06:29:54

+0

您正在獲取矢量中的數據,因爲它在數據庫中。並顯示我認爲這是它的名單?那麼最新的問題? – BBdev 2011-12-20 06:34:55

+0

ya exactly.Don't知道什麼是錯的 – j2me 2011-12-20 06:37:02