Last active
July 8, 2020 09:57
-
-
Save codenameone/9e2f7984beb22d9e372c to your computer and use it in GitHub Desktop.
Sample Usage of InfiniteContainer from Codename One fetches data dynamically from a webservice to scroll down infinitely
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void showForm() { | |
Form hi = new Form("InfiniteContainer", new BorderLayout()); | |
Style s = UIManager.getInstance().getComponentStyle("MultiLine1"); | |
FontImage p = FontImage.createMaterial(FontImage.MATERIAL_PORTRAIT, s); | |
EncodedImage placeholder = EncodedImage.createFromImage(p.scaled(p.getWidth() * 3, p.getHeight() * 3), false); | |
InfiniteContainer ic = new InfiniteContainer() { | |
@Override | |
public Component[] fetchComponents(int index, int amount) { | |
java.util.List<Map<String, Object>> data = fetchPropertyData("Leeds"); | |
MultiButton[] cmps = new MultiButton[data.size()]; | |
for(int iter = 0 ; iter < cmps.length ; iter++) { | |
Map<String, Object> currentListing = data.get(iter); | |
if(currentListing == null) { | |
return null; | |
} | |
String thumb_url = (String)currentListing.get("thumb_url"); | |
String guid = (String)currentListing.get("guid"); | |
String summary = (String)currentListing.get("summary"); | |
cmps[iter] = new MultiButton(summary); | |
cmps[iter].setIcon(URLImage.createToStorage(placeholder, guid, thumb_url)); | |
} | |
return cmps; | |
} | |
}; | |
hi.add(BorderLayout.CENTER, ic); | |
hi.show(); | |
} | |
int pageNumber = 1; | |
java.util.List<Map<String, Object>> fetchPropertyData(String text) { | |
try { | |
ConnectionRequest r = new ConnectionRequest(); | |
r.setPost(false); | |
r.setUrl("http://api.nestoria.co.uk/api"); | |
r.addArgument("pretty", "0"); | |
r.addArgument("action", "search_listings"); | |
r.addArgument("encoding", "json"); | |
r.addArgument("listing_type", "buy"); | |
r.addArgument("page", "" + pageNumber); | |
pageNumber++; | |
r.addArgument("country", "uk"); | |
r.addArgument("place_name", text); | |
NetworkManager.getInstance().addToQueueAndWait(r); | |
Map<String,Object> result = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(r.getResponseData()), "UTF-8")); | |
Map<String, Object> response = (Map<String, Object>)result.get("response"); | |
return (java.util.List<Map<String, Object>>)response.get("listings"); | |
} catch(Exception err) { | |
Log.e(err); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage code for InfiniteContainer.
From the Codename One project