Save file to Phone memory

Source: Internet
Author: User


A UI Code

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Tools:context= ". Mainactivity ">

<textview
Android:id= "@+id/tv_weather"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centerhorizontal= "true"
Android:layout_centervertical= "true"
/>
</RelativeLayout>

Two Logical Code

<?xml version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?>
<weather>
<day id= "1" >
<wendu>18</wendu>
<wind>5</wind>
<type> Sunny </type>
</day>
<day id= "2" >
<wendu>16</wendu>
<wind>3</wind>
<type> Rain </type>
</day>
<day id= "3" >
<wendu>19</wendu>
<wind>4</wind>
<type> Yin </type>
</day>
</weather>

Package com.itheima.weather.service;

Import Java.io.InputStream;
Import java.util.ArrayList;
Import java.util.List;

Import Org.xmlpull.v1.XmlPullParser;
Import org.xmlpull.v1.XmlPullParserException;

Import android.util.Xml;

Import Com.itheima.weather.domain.Weather;

public class WeatherService {
/**
* Parse to get weather information
* Weather information XML file corresponding to the stream
* @throws Exception
*/
public static list<weather> GetWeather (InputStream is) throws Exception {
Parse the weather XML file.
1. Gets the parser to an XML file.
Xmlpullparser parser = Xml.newpullparser ();
2. Initialize the parser.
Parser.setinput (IS, "utf-8");
3. Parse the XML file.
Gets the node type of the current parsing entry.
int eventtype = Parser.geteventtype (); The first time the call is positioned at the beginning of the XML
List<weather> Weatherinfos = null;
Weather weatherinfo = null;
while (eventtype! = xmlpullparser.end_document) {//need to keep the parser parsing the next node
Switch (eventtype) {
Case Xmlpullparser.start_tag:
if ("Weather". Equals (Parser.getname ())) {
Discovery Start node creates a collection for weather
Weatherinfos = new arraylist<weather> ();
} else if ("Day". Equals (Parser.getname ())) {
Find a new date that corresponds to the weather
Weatherinfo = new Weather ();
String id = parser.getattributevalue (0);
Weatherinfo.setid (Integer.parseint (id));
} else if ("Wendu". Equals (Parser.getname ())) {
String Wendu = Parser.nexttext ();
Weatherinfo.setwendu (Integer.parseint (Wendu));
} else if ("Wind". Equals (Parser.getname ())) {
String wind = Parser.nexttext ();
Weatherinfo.setwind (Integer.parseint (wind));
} else if ("type". Equals (Parser.getname ())) {
String type = Parser.nexttext ();
Weatherinfo.settype (type);
}
Break
Case Xmlpullparser.end_tag:
if ("Day". Equals (Parser.getname ())) {
Weatherinfos.add (Weatherinfo);
Weatherinfo=null;
}
Break
}
EventType = Parser.next ();//Control parser resolves the next node
}
Is.close ();
return Weatherinfos;
}
}

Package com.itheima.weather.domain;

public class Weather {
private int Wendu;
private int wind;
Private String type;
private int id;
public int Getwendu () {
return Wendu;
}
public void Setwendu (int wendu) {
This.wendu = Wendu;
}
public int Getwind () {
return wind;
}
public void Setwind (Int. wind) {
This.wind = Wind;
}
Public String GetType () {
return type;
}
public void SetType (String type) {
This.type = type;
}
public int getId () {
return ID;
}
public void setId (int id) {
This.id = ID;
}
@Override
Public String toString () {
Return "weather information [temperature =" + Wendu + ", wind =" + Winds + "level, weather condition =" + Type "
+ ", future first =" + ID + "Day";
}
}

Package com.itheima.weather;

Import java.util.List;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.TextView;
Import Android.widget.Toast;

Import Com.itheima.weather.domain.Weather;
Import Com.itheima.weather.service.WeatherService;

public class Mainactivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

TextView TV = (TextView) Findviewbyid (R.id.tv_weather);
try {
StringBuilder sb = new StringBuilder ();
list<weather> Weatherinfos = Weatherservice.getweather (getClassLoader (). getResourceAsStream ("Weather.xml")) ;
for (Weather Weather:weatherinfos) {
Sb.append (Weather.tostring ());
Sb.append ("\ n");
}
Tv.settext (Sb.tostring ());
} catch (Exception e) {
E.printstacktrace ();
Toast.maketext (This, "Parse weather information failed", 0). Show ();
}
}
}

Save file to Phone memory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.