儲存檔案到手機記憶體

來源:互聯網
上載者:User

標籤:


一 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>

 

二 Logical Code

<?xml version=‘1.0‘ encoding=‘utf-8‘ standalone=‘yes‘ ?>
<weather>
    <day id="1">
        <wendu>18</wendu>
        <wind>5</wind>
        <type>晴</type>
    </day>
    <day id="2">
        <wendu>16</wendu>
        <wind>3</wind>
        <type>雨</type>
    </day>
    <day id="3">
        <wendu>19</wendu>
        <wind>4</wind>
        <type>陰</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 {
    /**
     * 解析擷取天氣資訊
     * 天氣資訊xml檔案對應的流
     * @throws Exception
     */
    public static List<Weather> getWeather(InputStream is) throws Exception {
        // 解析 天氣的xml檔案.
        // 1.擷取到一個xml檔案的解析器.
        XmlPullParser parser = Xml.newPullParser();
        // 2.初始化解析器.
        parser.setInput(is, "utf-8");
        // 3.解析xml檔案.
        // 得到當前解析條目的節點類型.
        int eventType = parser.getEventType(); // 第一次被調用的時候 定位在xml開頭
        List<Weather> weatherInfos = null;
        Weather weatherInfo = null;
        while (eventType != XmlPullParser.END_DOCUMENT) {// 需要不停的讓 解析器解析下一個節點
            switch (eventType) {
            case XmlPullParser.START_TAG:
                if ("weather".equals(parser.getName())) {
                    // 發現開始節點 為weather 建立集合
                    weatherInfos = new ArrayList<Weather>();
                } else if ("day".equals(parser.getName())) {
                    // 發現一個新的日期 對應的天氣
                    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();// 控制解析器 解析下一個節點
        }
        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 "天氣資訊 [溫度=" + wendu + ", 風力=" + wind + "級 , 天氣狀況=" + type
                + ", 未來第=" + id + "天]";
    }
}

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, "解析天氣資訊失敗", 0).show();
        }
    }
}

儲存檔案到手機記憶體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.