2016-11-25 47 views
2

我正在用Codename One弄溼我的腳。我已經研究了更多的其他選項,如Xamarin,PhoneGap,Ionic跨平臺,但我有點吸引了Codename one,因爲它真的編碼一次,並在任何地方運行。codenameone Picker替代組合框

我已經經歷UI元素,我有點阻塞填充一個combobox(另一種方法是Picker

比方說,我店爲值對(storeIdstoreName)。我想在Picker中顯示storeName,但保留storeId作爲值參考。 選擇商店後,我想將storeId傳遞給API調用。
這是可能的。這可能是一個非常簡單的問題,但似乎有點難以實現(我對手機真的很陌生)。

謝謝。

回答

1

我們的建議是avoid ComboBox。這是一種UI本身並不存在的UI模式,在現代手機上會感覺陌生。它存在於Codename One中。

在從樣品這段代碼上面你可以得到一個複雜的多場組合框類似的效果:如果你堅持使用ComboBox你可以使用一個模型

Form hi = new Form("Button", BoxLayout.y()); 

String[] characters = { "Tyrion Lannister", "Jaime Lannister", "Cersei Lannister"}; 
String[] actors = { "Peter Dinklage", "Nikolaj Coster-Waldau", "Lena Headey"}; 
int size = Display.getInstance().convertToPixels(7); 
EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(size, size, 0xffcccccc), true); 
Image[] pictures = { 
    URLImage.createToStorage(placeholder, "tyrion","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/tyrion-lannister-512x512.jpg"), 
    URLImage.createToStorage(placeholder, "jaime","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/jamie-lannister-512x512.jpg"), 
    URLImage.createToStorage(placeholder, "cersei","http://i.lv3.hbo.com/assets/images/series/game-of-thrones/character/s5/cersei-lannister-512x512.jpg") 
}; 

MultiButton b = new MultiButton("Pick A Lanister..."); 
b.addActionListener(e -> { 
    Dialog d = new Dialog(); 
    d.setLayout(BoxLayout.y()); 
    d.getContentPane().setScrollableY(true); 
    for(int iter = 0 ; iter < characters.length ; iter++) { 
     MultiButton mb = new MultiButton(characters[iter]); 
     mb.setTextLine2(actors[iter]); 
     mb.setIcon(pictures[iter]); 
     d.add(mb); 
     mb.addActionListener(ee -> { 
      b.setTextLine1(mb.getTextLine1()); 
      b.setTextLine2(mb.getTextLine2()); 
      b.setIcon(mb.getIcon()); 
      d.dispose(); 
      b.revalidate(); 
     }); 
    } 
    d.showPopupDialog(b); 
}); 
hi.add(b); 
hi.show(); 

enter image description here

給它任何你想要的對象數據。然後創建一個單元格渲染器來顯示數據。這一切都在component section of Codname One's developer guide深入討論。請注意,由於ComboBox來自List大多數List提示和文檔適用於ComboBox

+0

謝謝Shai的快速回復。我意識到組合框不是我們要走的路,我喜歡Multibutton的想法。唯一不確定的是如何檢索選定項目的ID。 我相信應該有一種方法來獲取所選項目的值。我會試一試。 謝謝。 – Ravimaran

+0

你可以使用get/putClientProperty()在組件上存儲元數據看看我的答案在這裏:http://stackoverflow.com/questions/40774454/codenameone-refresh-list-of-images-without-reloading-the-頁面 –

+0

謝謝Shai。我會試一試。我一直很享受這一點。 – Ravimaran