1

Hi I'm using Codename One and Parse Server to save database in my Mobile App, but I wanna put each result of the query inside a button, because I need to click on each element of the List. ParseQuery.getQuery("List") "List" is the ID referenced in the database and "Title" return 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); }
Shai Almog
  • 51,749
  • 5
  • 35
  • 65
Simple
  • 827
  • 1
  • 9
  • 21
  • what is your question>? – Pankaj Gadge Mar 27 '18 at 20:46
  • How to get each element from results? Then add each element of results to a button. – Simple Mar 27 '18 at 20:51
  • I figured out this way in order to get the elements of results then put it in a Container. Is it correct? int index = 0; for(;index < results.size();++index) { dumpList.addComponent(ps.retrieveList(results.get(index).getString("Title"))); } listMenu.add(dumpList); – Simple Mar 28 '18 at 00:07
  • Added the parse4cn1 tag which you should use to get help on parse related questions – Shai Almog Mar 28 '18 at 02:37

1 Answers1

1

If you want a list of buttons you should probably do something like this:

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;
}

Notice you can use Button, MultiButton, SpanButton etc. for various use cases.

Notice that in the action listener you would want to invoke getActualComponent() on the event object and not getComponent().

E.g. event handling code:

 public void actionPerformed(ActionEvent ev) {
     MultiButton mb = ev.getActualComponent();
     ParseObject po = (ParseObject)mb.getClientProperty("ParseObject");
 }
Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • Thanks for the prompt reply, but after creating a button for each title, how to add all of them to the same listener? By the way does the ParseServer API work in the Codename One Simulator or does that only work from a finished build? Because when I try to do a request to my ParseServer hosting from the Simulator I got a failed request. – Simple Mar 28 '18 at 18:55
  • 1
    Updated my answer with generic action listener code and small tip. Parse should work with the simulator but you need to signup with a parse service provider for it to work – Shai Almog Mar 29 '18 at 04:47
  • Thank you very much, by the way I was having issue with the Simulator due to no CLIENT_KEY, since it says CLIENT_KEY is optional and can be null. So I didn't set it, but now I set it up and everything is working. – Simple Mar 29 '18 at 08:28