您好我正在使用Codename One和Parse Server在我的移动应用程序中保存数据库,但我想将查询的每个结果放在一个按钮中,因为我需要单击List的每个元素。 ParseQuery.getQuery(“List”)“List”是数据库中引用的ID,“Title”返回String。
//Method:
public Container retrieveList(String content) {
Container list = new Container(BoxLayout.y());
ParseObject po = null;
try {
po = ParseObject.fetch(content /*class name*/, "nsC2NdmCuQ" /*objectId*/);
} catch (ParseException e) {
Dialog.show("Err", "Oops! Database is not available at the moment" + e.getCode(), "OK", null);
}
Label title = new Label("Book's Title: " + po.getString("Title"));
list.addComponent(title);
return list;
}
//MENU:public void listMenu() {
final Form listMenu = new Form("Welcome to the List Menu"); listMenu.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); ParseQuery<ParseObject> query = ParseQuery.getQuery("List"); query.whereExists("Title"); List<ParseObject> results = null; Container dumpList = null; listMenu.add(dumpList).removeAll(); ParseServerDAO ps = new ParseServerDAO(); try { results = query.find(); int index = 0; for(;index < results.size();) { dumpList = ps.retrieveList(//How to get each element from results?); //Add each element of results to a button. } } catch (com.parse4cn1.ParseException e) { Dialog.show("Oops! Try later, server is not working right now.", "", "OK", null); } listMenu.add(dumpList); }
答案 0 :(得分:1)
如果你想要一个按钮列表,你应该做这样的事情:
public MultiButton retrieveListItem(String content, ActionListener l) {
ParseObject po = null;
try {
po = ParseObject.fetch(content /*class name*/, "nsC2NdmCuQ" /*objectId*/);
} catch (ParseException e) {
Dialog.show("Err", "Oops! Database is not available at the moment" + e.getCode(), "OK", null);
}
MultiButton title = new MultiButton("Book's Title: " + po.getString("Title"));
title.addActionListener(l);
title.putClientProperty("ParseObject", po);
return title;
}
请注意,您可以针对各种用例使用Button
,MultiButton
,SpanButton
等。
请注意,在动作侦听器中,您需要在事件对象上调用getActualComponent()
,而不在 getComponent()
。
E.g。事件处理代码:
public void actionPerformed(ActionEvent ev) {
MultiButton mb = ev.getActualComponent();
ParseObject po = (ParseObject)mb.getClientProperty("ParseObject");
}