【问题标题】:Codename One Parse Server to retrieve List代号一台解析服务器检索列表
【发布时间】:2018-09-06 09:42:30
【问题描述】:

您好,我正在使用 Codename One 和 Parse Server 将数据库保存在我的移动应用程序中,但我想将查询的每个结果放在一个按钮中,因为我需要单击列表的每个元素。 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;
}

//菜单:

公共无效列表菜单(){

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; for(;index
  • 添加了 parse4cn1 标签,您应该使用它来获取解析相关问题的帮助

标签: java list codenameone parse-server parse4cn1


【解决方案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;
}

请注意,您可以将ButtonMultiButtonSpanButton 等用于各种用例。

请注意,在动作侦听器中,您希望在事件对象上调用 getActualComponent() getComponent()

例如事件处理代码:

 public void actionPerformed(ActionEvent ev) {
     MultiButton mb = ev.getActualComponent();
     ParseObject po = (ParseObject)mb.getClientProperty("ParseObject");
 }

【讨论】:

  • 感谢您的及时回复,但是在为每个标题创建一个按钮后,如何将它们都添加到同一个侦听器中?顺便说一句,ParseServer API 是在 Codename One Simulator 中工作还是只能在完成的构建中工作?因为当我尝试从模拟器向我的 ParseServer 主机发出请求时,我收到了一个失败的请求。
  • 使用通用动作侦听器代码和小提示更新了我的答案。 Parse 应该可以与模拟器一起使用,但您需要向解析服务提供商注册才能使用它
  • 非常感谢,顺便说一下,由于没有 CLIENT_KEY,我遇到了模拟器问题,因为它说 CLIENT_KEY 是可选的并且可以为空。所以我没有设置它,但现在我设置它并且一切正常。
猜你喜欢
  • 2023-03-10
  • 2021-12-09
  • 1970-01-01
  • 2016-05-31
  • 1970-01-01
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
相关资源
最近更新 更多