Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this out for starters, then once you can do this, then move to using dynamic data. I used this for a very similar project and worked well:</p> <p><a href="http://www.ezzylearning.com/tutorial.aspx?tid=1763429" rel="nofollow">http://www.ezzylearning.com/tutorial.aspx?tid=1763429</a></p> <p>Here is the code I used. Maybe this will help you:</p> <pre><code>protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.dining); //Hard coded array ListData newListItem = new ListData(); ArrayList&lt;ListData&gt; itemObject=null; try { itemObject= newListItem.getData(DATAURL); Log.e(TAG, "item object: "+itemObject.toString()); ListAdapter adapter = new ListAdapter(this, R.layout.dininglistview, itemObject); //Create listview this.listView1 = (ListView)this.findViewById(android.R.id.list); this.listView1.setAdapter(adapter); } catch (InterruptedException e) { // TODO Auto-generated catch block Log.e(TAG, "Unexpected error", e); } catch (ServiceException e){ AlertDialog alertDialog = new AlertDialog.Builder(DiningActivity.this).create(); alertDialog.setTitle("Temporarily unavailable"); alertDialog.setMessage("Please contact top25@uievolution.com"); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent homeIntent = new Intent(DiningActivity.this, HomeActivity.class); DiningActivity.this.startActivity(homeIntent); } }); alertDialog.show(); } //implement dining adapter } </code></pre> <p>Here is the getData() method that I used to parse the XML:</p> <pre><code>public ArrayList&lt;ListData&gt; getData(String DATAURL) throws InterruptedException, ServiceException { ArrayList&lt;ListData&gt; items=null; HttpURLConnection conn = null; try { URL url = new URL(DATAURL); conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(TIMEOUT); conn.setConnectTimeout(CONNECT_TIMEOUT); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.connect(); if(Thread.interrupted()) { throw new InterruptedException(); } items = parseXML(conn); } catch (MalformedURLException e) { Log.e(TAG, "Invalid URL", e); } catch (IOException e) { // TODO Auto-generated catch block Log.e(TAG, "Unable to connect to URL", e); } finally{ if(conn!=null){ conn.disconnect(); } } return items; } </code></pre> <p>Here is my parser:</p> <pre><code>private static ArrayList&lt;ListData&gt; parseXML(HttpURLConnection conn) throws ServiceException { ArrayList&lt;ListData&gt; dataArray = new ArrayList&lt;ListData&gt;(); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = builderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { Log.e(TAG, "Parse Configuration issue", e); throw new ServiceException("Service Exception Error"); } catch (IllegalAccessError e){ Log.e(TAG, "Illegal Accessor Error", e); throw new ServiceException("Service Exception Error"); } try { //parse input from server Document document = builder.parse(conn.getInputStream()); Element xmlElement = document.getDocumentElement(); NodeList recordNodes = xmlElement.getChildNodes(); //assign parsed data to listItem object for(int i=0; i&lt;recordNodes.getLength(); i++){ Node record = recordNodes.item(i); NodeList recordDetails = record.getChildNodes(); ListData listItem = new ListData(); for(int ii=0; ii&lt;recordDetails.getLength(); ii++){ Node detailItem = recordDetails.item(ii); String detailType = detailItem.getNodeName(); String detailValue = detailItem.getTextContent(); //assign attributes to listItem object if(detailType.matches("item")){ int itemValue = Integer.parseInt(detailValue); listItem.setItem(itemValue); } else if(detailType.matches("title")){ listItem.setTitle(detailValue); } else if(detailType.matches("subhead")){ listItem.setSubhead(detailValue); } else if(detailType.matches("thumb")){ ImageManager im = new ImageManager(); Bitmap tImg = im.getImage(detailValue); listItem.setThumb(tImg); } else if(detailType.matches("photo")){ ImageManager im = new ImageManager(); Bitmap pImg = im.getImage(detailValue); listItem.setPhoto(pImg); } else if(detailType.matches("localAddress")){ listItem.setLocalAddress(detailValue); } else if(detailType.matches("phone")){ listItem.setPhone(detailValue); } else if(detailType.matches("webUrl")){ URL webUrl1 = new URL(detailValue); listItem.setWebURL(webUrl1); } else if(detailType.matches("facebook")){ listItem.setFacebook(detailValue); } else if(detailType.matches("twitter")){ listItem.setTwitter(detailValue); } else if(detailType.matches("latitude")){ float itemLat = Float.parseFloat(detailValue); listItem.setLatitude(itemLat); } else if(detailType.matches("longitude")){ float itemLon = Float.parseFloat(detailValue); listItem.setLatitude(itemLon); } else if(detailType.matches("notes")){ listItem.setNotes(detailValue); } else if(detailType.matches("comments")){ listItem.setComments(detailValue); } } dataArray.add(listItem); } } catch (SAXException e) { //TODO e.printStackTrace(); } catch (IOException e) { //TODO e.printStackTrace(); } return dataArray; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload