Weather Forecast App---Tutorial II

Source: Internet
Author: User

The second part is to choose the city, then get the data this piece.

First of all the national cities, we certainly can not write in the program, such as the number of storage, so we should download a National City XML data, and then placed in the Android assents directory, and then parse the XML to get provinces and cities. Here I have chosen sax parsing.

SAXParser saxparser=saxparserfactory.newinstance (). Newsaxparser ();            Saxparser.parse (Getresources (). Getassets (). Open ("Citys.xml"), MyHandler);

Open a stream through the assents directory and then parse the core function with sax.

With sax parsing, we can get provinces and cities here I used HashMap to store the map{{province},{arraylist<city>}}. In this way, we can take the city directly out of the province to facilitate the subsequent selection of the province after the corresponding city, forming a listvie linkage

 Public classMyHandlerextendsdefaulthandler{ PublicArrayList provice=NewArrayList ();  PublicArrayList item=NewArrayList ();  PublicHashmap<string,arraylist> items=NewHashMap (); String beginstring=""; @Override Public voidStartelement (String uri, String localname, String qName, Attributes Attributes)throwssaxexception {Super. Startelement (URI, LocalName, qName, attributes); if("Province". Equals (LocalName)) {Provice.add (Attributes.getvalue (0)); Beginstring=attributes.getvalue (0); }} @Override Public voidCharacters (Char[] ch,intStartintLengththrowssaxexception {Super. Characters (ch, start, length); String Temp=NewString (ch,start,length); if(!temp.trim (). Equals ("") &&!temp.trim (). Equals ("\ n") {item.add (temp); }} @Override Public voidEndElement (String uri, String localname, String qName)throwssaxexception {Super. EndElement (URI, LocalName, QName); if("Province". Equals (LocalName))            {Items.put (beginstring, item); Item=NewArrayList (); }    }}

Above is the method implementation of Sax parsing. Now we are missing something, the first one is to click on the button to switch to the next screen to select the city, then return to the city, show the weather. The second one is to save the city and load it the next time.

The first question is relatively simple startactivityforresult, this method can solve the problem, the second is to save the file, note that some phones may not have an external memory card, so we'd better put the file in the corresponding directory of the application, The Openfileoutput () and Openfileinput () method that comes with Android can solve this problem

Weather Forecast App---Tutorial II

Related Article

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.