Advertisement
Guest User

BillsDetailsActivity.java

a guest
Mar 12th, 2013
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.77 KB | None | 0 0
  1. package com.example.beaconhomes;
  2.  
  3. import java.net.MalformedURLException;
  4. import java.net.URL;
  5. import java.util.ArrayList;
  6.  
  7. import android.app.Activity;
  8. import android.content.Intent;
  9. import android.os.AsyncTask;
  10. import android.os.Bundle;
  11. import android.sax.Element;
  12. import android.sax.EndElementListener;
  13. import android.sax.EndTextElementListener;
  14. import android.sax.RootElement;
  15. import android.util.Xml;
  16. import android.view.Menu;
  17. import android.view.MenuItem;
  18. import android.widget.TextView;
  19.  
  20. public class BillsDetailsActivity extends Activity {
  21.    
  22.     private ArrayList<Itemisedbill> itemisedbill;
  23.  
  24.     @Override
  25.     protected void onCreate(Bundle savedInstanceState) {
  26.         super.onCreate(savedInstanceState);
  27.         setContentView(R.layout.activity_bills_details);
  28.        
  29.         Intent intent = getIntent();
  30.         bills bill = (bills)intent.getSerializableExtra("bill");
  31.        
  32.         new DownloaditemisedbillListTask().execute("http://www.fcet.staffs.ac.uk/pfb1/IMWTAssignment/"+bill.getItemisedBill());
  33.        
  34.     }
  35.  
  36.     @Override
  37.     public boolean onCreateOptionsMenu(Menu menu) {
  38.         // Inflate the menu; this adds items to the action bar if it is present.
  39.         getMenuInflater().inflate(R.menu.activity_bills_details, menu);
  40.         return true;
  41.     }
  42.  
  43.     @Override
  44.     public boolean onOptionsItemSelected(MenuItem item) {
  45.         switch (item.getItemId()) {
  46.         case android.R.id.home:
  47.             Intent intent = new Intent(this, BillsActivity.class);
  48.             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  49.             startActivity(intent);
  50.             return true;
  51.         }
  52.         return super.onOptionsItemSelected(item);
  53.     }
  54.    
  55.     //The method to get the bills out of our XML file
  56.         public ArrayList<Itemisedbill> ParseitemisedbillFromXML(String XMLURL)
  57.         {
  58.             //Our temporary variables - these must be final to guarantee we don't change them
  59.             final ArrayList<Itemisedbill> values = new ArrayList<Itemisedbill>();
  60.             final Itemisedbill currentbill = new Itemisedbill();
  61.            
  62.             //Convert our string into a URL
  63.             URL feedUrl;
  64.             try {
  65.                 feedUrl = new URL(XMLURL);
  66.             } catch (MalformedURLException e) {
  67.                  throw new RuntimeException(e);
  68.             }
  69.            
  70.             //Get the RootElement of our XML file which is bill
  71.             RootElement root = new RootElement("itemisedbill");
  72.             Element bill = root.getChild("itemisedbill"); //Then list our element we want, so a single bill
  73.            
  74.             //Our End Element Listener event fires when we get the end of a complete bill tag
  75.             bill.setEndElementListener(new EndElementListener(){
  76.                 @Override
  77.                 public void end() {
  78.                     values.add(currentbill.copy());
  79.                 }
  80.             });
  81.            
  82.             //All of the End Text Element Listeners get the body of the child tags of a bill
  83.             bill.getChild("item").setEndTextElementListener(new EndTextElementListener(){
  84.                 @Override
  85.                 public void end(String body) {
  86.                     currentbill.setperiodFrom(body);
  87.                 }
  88.             });
  89.             bill.getChild("periodFrom").setEndTextElementListener(new EndTextElementListener(){
  90.                 @Override
  91.                 public void end(String body) {
  92.                     currentbill.setperiodFrom(body);
  93.                 }
  94.             });
  95.             bill.getChild("periodTo").setEndTextElementListener(new EndTextElementListener(){
  96.                 @Override
  97.                 public void end(String body) {
  98.                     currentbill.setperiodTo(body);
  99.                 }
  100.             });
  101.             bill.getChild("cost").setEndTextElementListener(new EndTextElementListener(){
  102.                 @Override
  103.                 public void end(String body) {
  104.                     currentbill.setcost(body);
  105.                 }
  106.             });
  107.             bill.getChild("amountUsed").setEndTextElementListener(new EndTextElementListener(){
  108.                 @Override
  109.                 public void end(String body) {
  110.                     currentbill.setamountUsed(body);
  111.                 }
  112.             });
  113.            
  114.            
  115.            
  116.             //Now we have set up all our event listeners, we can start to parse the document
  117.             try {      
  118.                 Xml.parse(feedUrl.openConnection().getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
  119.             } catch (Exception e) {
  120.                 throw new RuntimeException(e);
  121.             }
  122.            
  123.             //We can then return all our bills once we have parsed the XML
  124.             return values;
  125.         }
  126.            
  127.         //This is an inner class which runs Asynchronously (In the background)
  128.         //It needs to be told the three types it accepts
  129.         //String is the type that is passed to doInBackground
  130.         //Void is the type that is used if we report any progress (We don't in this case so its Void!)
  131.         //ArrayList<bill> is the data type that is returned from our background task and is passed to final function
  132.         class DownloaditemisedbillListTask extends AsyncTask<String, Void, ArrayList<Itemisedbill>> {
  133.                
  134.             //This is the code that runs in the background - We can't update any UI in this function!
  135.             //String... basically means we are passing in a list of strings but we don't specify how many!
  136.             @Override
  137.             protected ArrayList<Itemisedbill> doInBackground(String... arg0) {
  138.                
  139.                 //This calls our Parse function above
  140.                 return ParseitemisedbillFromXML(arg0[0]);
  141.             }
  142.  
  143.             //This executes back on the UI thread so we can actually update the UI here
  144.             protected void onPostExecute(ArrayList<Itemisedbill> result) {
  145.                 //Save our result to our class level bill ArrayList
  146.                 itemisedbill = result;
  147.                
  148.                 TextView tv = (TextView)findViewById(R.id.bill_periodFrom);
  149.                 tv.setText(itemisedbill.getperiodFrom());
  150.                 tv = (TextView)findViewById(R.id.bill_periodTo);
  151.                 tv.setText(itemisedbill.getperiodTo());
  152.                 tv = (TextView)findViewById(R.id.bill_cost);
  153.                 tv.setText(itemisedbill.getcost());
  154.                 tv = (TextView)findViewById(R.id.bill_amountUsed);
  155.                 tv.setText(itemisedbill.getamountUsed());
  156.              }
  157.            
  158.         }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement