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