Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MainActivity.java
- =======================
- package com.example.myxml;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.os.AsyncTask;
- import android.provider.DocumentsContract;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.ListView;
- import org.w3c.dom.Document;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.StringReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- public class MainActivity extends AppCompatActivity {
- //declate a collection of currencies, to hold each currency data
- private List<ClsCur> myCurList;
- //declare of xmlString which will get our string from the internet
- String xmlString="";
- //our beloved context
- Context context;
- //declare of list view in xml
- ListView myListView;
- public final String bankURL="https://www.boi.org.il/currency.xml";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- readXML();
- }
- @SuppressLint("StaticFieldLeak")
- private void readXML() {
- xmlString="";
- //we next ever never using void , we need to use Void
- new AsyncTask<Void,Void,String>(){
- @Override
- protected String doInBackground(Void... voids) {
- //we need to open HTTP URL connection to our desired URL (www.boi.org.il)
- HttpURLConnection connection = null;
- try {
- connection = (HttpURLConnection) new URL(bankURL).openConnection();
- BufferedReader buf = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String line;
- while ((line=buf.readLine())!=null){
- xmlString+=line;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return xmlString;
- }
- @Override
- protected void onPostExecute(String s) {
- //Log.e("XML", "onPostExecute: "+s);
- parseXML(s);
- }
- }.execute();
- }
- private void parseXML(String xmlData) {
- //we will create a document builder that will allow us an easier access to our XML String
- DocumentBuilder builder;
- try {
- builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- Document DOM = builder.parse(new InputSource(new StringReader(xmlData)));
- //our xml structure
- NodeList itemName = DOM.getElementsByTagName("NAME");
- NodeList itemUnit = DOM.getElementsByTagName("UNIT");
- NodeList itemCode = DOM.getElementsByTagName("CURRENCYCODE");
- NodeList itemCountry = DOM.getElementsByTagName("COUNTRY");
- NodeList itemRate = DOM.getElementsByTagName("RATE");
- NodeList itemChange = DOM.getElementsByTagName("CHANGE");
- //let got on our entire collection to get the data
- for (int index=0;index<itemName.getLength();index++){
- myCurList.add(new ClsCur(
- itemName.item(index).getTextContent(),
- Integer.parseInt(itemUnit.item(index).getTextContent()),
- itemCode.item(index).getTextContent(),
- itemCountry.item(index).getTextContent(),
- Float.parseFloat(itemRate.item(index).getTextContent()),
- Float.parseFloat(itemChange.item(index).getTextContent())
- ));
- }
- /*
- for (ClsCur item:myCurList){
- Log.e("cur", "parseXML: "+item.getCurrencyCode()+" "+item.getRate() );
- }
- */
- //create adapter and assign it to the list view
- CurAdapter adapter = new CurAdapter(context,myCurList);
- myListView.setAdapter(adapter);
- } catch (ParserConfigurationException | SAXException | IOException e) {
- e.printStackTrace();
- }
- }
- private void setPointer() {
- this.context=this;
- myCurList = new ArrayList<>();
- myListView=findViewById(R.id.lstCur);
- }
- }
- activity_main.xml
- ==================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="32sp"
- android:text="Current Currencies"
- android:gravity="center"
- />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="3dp"
- android:background="#000"
- />
- <ListView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/lstCur"/>
- </LinearLayout>
- CurAdapter.java
- ==================
- package com.example.myxml;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.graphics.Color;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
- import java.util.List;
- public class CurAdapter extends BaseAdapter {
- Context context;
- List<ClsCur> myCurList;
- public CurAdapter(Context context, List<ClsCur> myCurList) {
- this.context = context;
- this.myCurList = myCurList;
- }
- @Override
- public int getCount() {
- return myCurList.size();
- }
- @Override
- public Object getItem(int position) {
- return null;
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- @SuppressLint("SetTextI18n")
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View myView = LayoutInflater.from(context).inflate(R.layout.cur_item,null);
- ((TextView)myView.findViewById(R.id.txtName)).setText(myCurList.get(position).getName());
- ((TextView)myView.findViewById(R.id.txtCurCode)).setText(myCurList.get(position).getCurrencyCode());
- ((TextView)myView.findViewById(R.id.txtRate)).setText(myCurList.get(position).getRate()+"");
- ((TextView)myView.findViewById(R.id.txtChange)).setText(myCurList.get(position).getChange()+"");
- ((TextView)myView.findViewById(R.id.txtChange)).setTextColor(myCurList.get(position).getRate()>0? Color.RED:Color.GREEN);
- return myView;
- }
- }
- cur_item.xml
- ================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="MOCK DATA"
- android:layout_marginBottom="10dp"
- android:layout_weight="1"
- android:id="@+id/txtName"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="MOCK DATA"
- android:layout_marginBottom="10dp"
- android:layout_weight="1"
- android:id="@+id/txtCurCode"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="MOCK DATA"
- android:layout_marginBottom="10dp"
- android:layout_weight="1"
- android:id="@+id/txtRate"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:text="MOCK DATA"
- android:layout_marginBottom="10dp"
- android:layout_weight="1"
- android:id="@+id/txtChange"/>
- </LinearLayout>
- ClsCur.java
- ================
- package com.example.myxml;
- public class ClsCur {
- String name;
- int unit;
- String currencyCode;
- String country;
- float rate;
- float change;
- public ClsCur(String name, int unit, String currencyCode, String country, float rate, float change) {
- this.name = name;
- this.unit = unit;
- this.currencyCode = currencyCode;
- this.country = country;
- this.rate = rate;
- this.change = change;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getUnit() {
- return unit;
- }
- public void setUnit(int unit) {
- this.unit = unit;
- }
- public String getCurrencyCode() {
- return currencyCode;
- }
- public void setCurrencyCode(String currencyCode) {
- this.currencyCode = currencyCode;
- }
- public String getCountry() {
- return country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- public float getRate() {
- return rate;
- }
- public void setRate(float rate) {
- this.rate = rate;
- }
- public float getChange() {
- return change;
- }
- public void setChange(float change) {
- this.change = change;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement