Subversion Repositories ES

Compare Revisions

Last modification

Ignore whitespace Rev 14 → Rev 15

/trunk/src/de/pointedears/converter/app/CurrenciesActivity.java
23,6 → 23,7
import android.widget.TextView;
import de.pointedears.converter.R;
import de.pointedears.converter.db.CurrenciesDatabase;
import de.pointedears.converter.helpers.CurrenciesUpdateThread;
 
/**
* Activity that implements currency conversion
278,15 → 279,19
*
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
/*
* (non-Javadoc)
*
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
/* update here */
return super.onOptionsItemSelected(item);
// Handle item selection
switch (item.getItemId())
{
case R.id.item_options_update:
Thread updateThread = new CurrenciesUpdateThread();
updateThread.start();
return true;
 
default:
return super.onOptionsItemSelected(item);
}
}
}
/trunk/src/de/pointedears/converter/helpers/CurrenciesUpdateThread.java
0,0 → 1,105
/**
* Defines a class to update table rates in the background
*/
package de.pointedears.converter.helpers;
 
import java.io.IOException;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
 
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
 
/**
* @author pelinux
*
*/
public class CurrenciesUpdateThread extends Thread
{
private static final String URL_ECB =
"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$
 
/**
* Default constructor which sets the runnable
*
* @param runnable
*/
private CurrenciesUpdateThread(Runnable runnable)
{
super(runnable);
}
 
/**
* Default constructor
*/
public CurrenciesUpdateThread()
{
this(new Runnable() {
@Override
public void run()
{
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
try
{
DocumentBuilder builder =
documentBuilderFactory.newDocumentBuilder();
try
{
Document doc = builder.parse(CurrenciesUpdateThread.URL_ECB);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
try
{
XPathExpression expr = xpath.compile("/Cube/Cube//Cube"); //$NON-NLS-1$
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0, len = nodes.getLength(); i < len; i++)
{
Node item = nodes.item(i);
NamedNodeMap attributes = item.getAttributes();
String currency =
attributes.getNamedItem("currency").getNodeValue(); //$NON-NLS-1$
String rate = attributes.getNamedItem("rate").toString(); //$NON-NLS-1$
 
/* TODO: Update UI */
System.out.println(currency + ": " + rate); //$NON-NLS-1$
}
}
catch (XPathExpressionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
Property changes:
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property