Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.beaconhomes;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.sax.Element;
- import android.sax.EndElementListener;
- import android.sax.EndTextElementListener;
- import android.sax.RootElement;
- import android.util.Xml;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
- public class BillsDetailsActivity extends Activity {
- private ArrayList<Itemisedbill> itemisedbill;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_bills_details);
- Intent intent = getIntent();
- bills bill = (bills)intent.getSerializableExtra("bill");
- new DownloaditemisedbillListTask().execute("http://www.fcet.staffs.ac.uk/pfb1/IMWTAssignment/"+bill.getItemisedBill());
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_bills_details, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case android.R.id.home:
- Intent intent = new Intent(this, BillsActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- //The method to get the bills out of our XML file
- public ArrayList<Itemisedbill> ParseitemisedbillFromXML(String XMLURL)
- {
- //Our temporary variables - these must be final to guarantee we don't change them
- final ArrayList<Itemisedbill> values = new ArrayList<Itemisedbill>();
- final Itemisedbill currentbill = new Itemisedbill();
- //Convert our string into a URL
- URL feedUrl;
- try {
- feedUrl = new URL(XMLURL);
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- }
- //Get the RootElement of our XML file which is bill
- RootElement root = new RootElement("itemisedbill");
- Element bill = root.getChild("itemisedbill"); //Then list our element we want, so a single bill
- //Our End Element Listener event fires when we get the end of a complete bill tag
- bill.setEndElementListener(new EndElementListener(){
- @Override
- public void end() {
- values.add(currentbill.copy());
- }
- });
- //All of the End Text Element Listeners get the body of the child tags of a bill
- bill.getChild("item").setEndTextElementListener(new EndTextElementListener(){
- @Override
- public void end(String body) {
- currentbill.setperiodFrom(body);
- }
- });
- bill.getChild("periodFrom").setEndTextElementListener(new EndTextElementListener(){
- @Override
- public void end(String body) {
- currentbill.setperiodFrom(body);
- }
- });
- bill.getChild("periodTo").setEndTextElementListener(new EndTextElementListener(){
- @Override
- public void end(String body) {
- currentbill.setperiodTo(body);
- }
- });
- bill.getChild("cost").setEndTextElementListener(new EndTextElementListener(){
- @Override
- public void end(String body) {
- currentbill.setcost(body);
- }
- });
- bill.getChild("amountUsed").setEndTextElementListener(new EndTextElementListener(){
- @Override
- public void end(String body) {
- currentbill.setamountUsed(body);
- }
- });
- //Now we have set up all our event listeners, we can start to parse the document
- try {
- Xml.parse(feedUrl.openConnection().getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- //We can then return all our bills once we have parsed the XML
- return values;
- }
- //This is an inner class which runs Asynchronously (In the background)
- //It needs to be told the three types it accepts
- //String is the type that is passed to doInBackground
- //Void is the type that is used if we report any progress (We don't in this case so its Void!)
- //ArrayList<bill> is the data type that is returned from our background task and is passed to final function
- class DownloaditemisedbillListTask extends AsyncTask<String, Void, ArrayList<Itemisedbill>> {
- //This is the code that runs in the background - We can't update any UI in this function!
- //String... basically means we are passing in a list of strings but we don't specify how many!
- @Override
- protected ArrayList<Itemisedbill> doInBackground(String... arg0) {
- //This calls our Parse function above
- return ParseitemisedbillFromXML(arg0[0]);
- }
- //This executes back on the UI thread so we can actually update the UI here
- protected void onPostExecute(ArrayList<Itemisedbill> result) {
- //Save our result to our class level bill ArrayList
- itemisedbill = result;
- TextView tv = (TextView)findViewById(R.id.bill_periodFrom);
- tv.setText(itemisedbill.getperiodFrom());
- tv = (TextView)findViewById(R.id.bill_periodTo);
- tv.setText(itemisedbill.getperiodTo());
- tv = (TextView)findViewById(R.id.bill_cost);
- tv.setText(itemisedbill.getcost());
- tv = (TextView)findViewById(R.id.bill_amountUsed);
- tv.setText(itemisedbill.getamountUsed());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement