Iyk

currentstate = R3S LINK; return; \

if (localName.equals("category")) (

currentstate RSS CATEGORY;

if (localName.equals("pubDate")) <

currentstate = RSS PUBDATE; return; J

public void endElement(String namespaceURI, String localName, String qName) throws

SAXException {

//ta&Mtir—t item fttft RSSItem RSSFeed ^o if (localName.equals("item")) {

RSSFeed.add Item(RSSItem); return; )

public void characters(char ch IJ, int start, int length) (

String theString = new String(ch,start,length);

switch (currentstate) {

case RSSTITLE:

RSSItem.setTitle(theString) ;

currentstate = 0;

break;

case RSS LINK:

RSSItem.setLink(theString);

currentstate 0;

break;

case RSS_DESCRIPTION:

RSSItem.setDescription(theString);

currentstate = 0;

break;

case RSS_CATEGORY:

RSSItem.setCategory(theString);

currentstate = 0;

break;

case RSS PUBDATE: RSSItem.setPubDate(theString); currentstate = 0; break; default: return; 1 ) I

ttfttiämmftfö, '^ContentMandler-fKöiWFhWT/L

(1) ® "/b: a^h^^^DefaultHandlerc DefaultHandler

ContentHandler. RMRmsmnmULffymttfeW»!.

(2) 'M :>P: i;'^ startI)ocument()ili endDocumentOW^^vio —Mt/rZHUff^J Ä %)} in it fKl X \\- IS. S'J startDocument()ii ^ jüfe • Wx\\< Ä^^TMÜ. ftflftft^Sii tijtfUt r RSS Feed fll RSSItem, nj^^-^i^MW XfHX&SU endDocument()#/£!fi&tiI, AiÄ^W

(3 ) tß—.'J^: startHlement (String namespaceURI, String localNamc,String qName, Attributes aus) Mmo XML »rrSiSi»! XML ima^M tag ffusmv&^mm* fstt&Ä^B»»

M&i&H. localNamc a^ftiSiT^JKifiift Win, S + ftllfi localName

J£"ItcmM, currentstate X/ Item, MRiES»®—^ Item WMQMM

mi Item

(4) ffi^ characters (char ch[], int start, int length) Jäo ^WAtt. -iM

WS&i'rÄ startElemento;/vijn, (WUr^tr/Stört»fitt^ttirört^ife. # MMfc ch[]lt£ trjS WrtS. /h^tfr'l f^'p, ftifj«« currentstate föl^PJ. tag^tjÄWÄ». «

( 5 ) li.0: ft'1! endElement (String namespaceURi, String localName, String qName) f/iio startEIementOA-fiXihVlo SMflbc tag tYtfJS. - «Ji^«^^»»

fWflr ' t Item -ftj&iSjfc, RSSItem RSSFeed 't'o

12.4.3 Activity ft^Sl 1. ActivityMain

SMJFSM««. mV&^MfrM AP Activiy Activity Main, Activity ^»ilj 7» &

SSiij RSS feed, W UstView (W^M^ft*. ftttftWiH K:

package com.RSS reader;

import java.net.URL;

import, javax . xml .parsers . SAXParser; import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource; import, org. xml. sax . XMLReader;

import. Android. app . Act ivity; import Android.content.Intent; import Android.os.Bundle; import Android.view.View; import Android.widget.AdapterView; import Android.widget.ListView; import Android, widget.. SimpleAdapt.er;

import Android.widget.AdapterView.OnItemClickListener;

import corn. RSS reader . data . RSSFeed; import com.RSS_reader.data.RSSItem; import com.RSS reader.sax.RSSHandler;

public class ActivityMain extends Activity implements OnItemClickListener (

public final String RSS_URL = "http://rubyjin.cn/blog/RSS"; public final String tag = "RSSReader"; private RSSFeed feed = null;

/"* Called when the activity is first created. */

public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); feed = getFeed(RSS URL); showListView();

RSSFeed private RSSFeed getFeed(String urlString) (

URL url new URL(urI St ring);

SAXParserFactory factory = SAXParserFactory.newlnstance(); SAXParser parser factory.newSAXParser(); XMLReader xmlreader = parser.getXMLReader();

RSSHandler RSSHandler = new RSSHandler();

xmlreader.setContent Handler(RSSHandler);

InputSource is = new Input Source(ur l.openStream()); xmlreader.parse (is);

return RSSHandler.getFeed();

catch (Exception ee) (

return null; I

private void showListView() (

ListView itemlist (ListView) findViewByld(R.id.itemlist);

return;

SimpleAdapter adapter = new SimpleAdapter(this, teed.getAl11temsForList View(), Android.R.layout.simple_list item 2, newStringU { RSSItem.TITLE, RSS I tens. PUBDATE ), new int[| \ Android.R.id.text I , Android.R.id.text2}); item]ist.setAdapter(adapter); i temlist.setOn:temClickListener (this); item] ist. setSe -.ection (0) ;

public void onltemClick(AdapterView parent, View v, int position, long id) (

Intent itemintent = new Intent(this,ActivityShowDescription.class); Bundle b new Bundle();

b.putString("tit 1e", feed.get I tern(position) .getTitle()); b.putString("description", feed.get I tern(position) .getDescription() ) ; b.putString("link", feed.getI tern(position) .getLink() ) ; b.putString("pubdate", feed.getItem(position).getPubDate());

itemintent.putExt ra("Android.intent.extra.RSSItem", b); startActivityForResult(itemintent, 0);

• tfgctFec^)«»ai4ftffmw^»*««rRssfccd, »RutttTmvi. vmrnm^'®-

RSSFeed W

• showListView()^5&#®;& RSSFeed ii0<JF«3S W ListView toMJU&W&te* ffiflJ ih ListView ill—^ SimpleAdapter itfrSf

• III Y ActivityMain MI OnltemClickListener & I 1, ff\ WitWft ListView MiiJiW

Vmm onItemClick()ffi»ffiiA.# onItemClick()^^ liift Item ffj^SSfcii Extra VI', mis mi Activity mwu

2. ActivityShowDescription Eft SB

-J'jiiuiitA^ -JJ[ffiiitill fcS#[ill, iict ^Iflii11 ActivityShowDescription immm K:

package com.RSS reader;

import Android.app.Act ivity; import Android.os.Bundle; import Android.widget.Button; import Android.widget-TextView; import Android, content. Intents-import Android.view.*;

public class ActivityShowDescription extends Activity I public void onCreate(Bundle icicle) I super.onCreate{icicle) ;

setContentView<R.layout.showdescription) ;

String content = null;

Intent startinglntent = getlntentO;

if (startinglntent != null) { Bundle bundle = startinglntent

. get.BundleExtra ("Android, intent .extra.RSSItem") ; if (bundle == null) ( content = "WS&Jff ;

content = bundle . getStnng ("title") + "\n\n" + bundle.getString("pubdate") + "\n\n" »• bundle.getString("description").replace('\n' , ' ') + "\ri\n + bundle.getString("link");

TextView textView - (TextView) findViewByld(R.id.content); text View. setText (content) ;

Button backbutton = (Button) findViewByld(R.id.back) ;

backbutton.setOnClickListener(new Button.OnClickListener0 ( public void onClick(View v) {

• A: onCrcate()is63KM. «MiJMJ^ji^ Activity intent Extra inM» f&Sft&til

\Yi rt /& 81 ' ^ TextVeiw it \ f M A< • ¡¿.^ Activity rttl&fft layout ftnT:

<?xml version="l.0" encoding="utf-8"?>

<LinearLayout xmlns:And-oid="http://schemas.Android.com/apk/res/Android" Android:orientation="vertical" Android:1ayout width="fill_parent"

Android:layout height="fi1l_parent"

<Text:View

Android:layout width="fill parenL" Android:layout_height="wrap_content" Android:autoLink="all" Android:text="" Android: id="0 + id/cont.ent" Android:layout weight^"1.0" /> <Button

Android:layout width="fillparent" Android:layout_height="wrap_cbntent" Android: text = "i6®" Android:id="@+id/back" />

</LinearLayout>

RSS, il lJA SAX,, Android «SEfittlHW SAX M^ftW RSS fft&ffi, ilflMM

it aif jfi) »1tt »'¥-». tf Andriod M & ft Bft »» 7. Xi j' £ » fft ft! mi ifii i><j m«B

Cioogle Map L.

h'i&iiiV.J-t Service, JEW« GPS N fWrfcHMMMfrU.. fcltfcASKSJi'f. «1

plimSffWttMfflSHBSk. ftjtfiaM^ft Map t. WfcnTlilliaafcffl^iiitoftR.

«f11 nIW J&IHMfit6tiill F/l/H^SS:

itx-tft, «¡fir, mr.ma ft.. M 13-i ßr'K&mZmtiM hßijflSBIRlBiiW.

main fitöffiM kSflfll, fflfcft kiftiäwJlifi 10 &fl<j®fö iiiijk, ßüW, Jt- Clist), il-iß^illJLt m>(mmm!±"^ahmt."mitui.r jSS*fi#FlÜJlL ff.ÄJ;

tÄfiat-t, üTWirjr-iCM^-^ifff, /iJtM'jiVr-iin I.. •niiiltkW, it-tttM l-^iilil',ÄXfj&töffilS&i, ifijItlK^'T'-Ni wssjfeiw®^ -«ms.. m^z nmft, fäittzft. H^iJur-föffi r ^ttttamm ffiiit^ifii i-, fflpnrwsäiÄGPs sfrMis*. ISL&K&M M*Amiuttt9tt«. Ai/ri. föt»; mna, mmm)MM. f$it»xm&ij rnvL a-SKW -t» "«WM" « b^ÄWJMffliißHv*. iaim 13-1 m*.

0 0

Post a comment