2012-04-27 99 views
0

我使用sax解析器而不是列出所有我想一次拉出一條記錄的所有內容。這裏是我的xml我是從sax xml隨機顯示一個項目

<?xml version="1.0" encoding="UTF-8" ?> 
<rss version="2.0"> 
<channel> 
    <title>Conservation Tips</title> 
    <link>blah</link> 
    <description>Living comfortably during a Memphis summer can be challenging, but it does not have to be costly. What are some of the easiest ways to stay cool and save?</description> 
    <item> 
     <title>Have a professional, reputable contractor clean and inspect your air conditioner. This should be done every year, whether you have window or central units.</title> 
    </item> 
    <item> 
     <title>Set the thermostat at 78 degrees or higher for the most energy efficient operation. Each degree below this setting adds 6% to your cooling costs.</title> 
    </item> 
    <item> 
     <title>Check your air conditioner's filter every time you receive your utility bill.</title> 
     </item> 
    <item> 
     <title>Use fans to move the air inside your home. This gives the sensation that it is 5 degrees cooler than the actual temperature.</title> 
     </item> 
    <item> 
     <title>Shade windows on the sunny side of your home.</title> 
     </item> 
    <item> 
     <title>Keep drapes closed or add room-darkening shades to block out the heat from the sun.</title> 
     </item> 
    <item> 
     <title>The outside portion of a central air conditioner is the condensing unit. Keep it clear from dried mud, debris and grass clippings, because it needs to breathe.</title> 
     </item> 
    <item> 
     <title>Use your programmable thermostat to automatically increase the temperature setting at bedtime.</title> 
     </item> 
    <item> 
     <title>Sleep under lightweight bedding and use fans during sleep.</title> 
     </item> 
    <item> 
     <title>Do not place lamps near your thermostat. The thermostat senses the heat produced from the lamp and causes the air conditioner to run longer than necessary.</title> 
     </item> 
    <item> 
     <title>Do not set your thermostat at a colder setting than normal when you turn on your air conditioner. It will not cool your home any faster and could result in excessive cooling and, therefore, unnecessary expense.</title> 
     </item> 
</channel> 

拉動和這裏,我有

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    try { 
     url = new URL("url"); 
    } catch (MalformedURLException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    RssFeed feed = null; 
    try { 
     feed = RssReader.read(url); 
    } catch (SAXException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    ArrayList<RssItem> rssItems = feed.getRssItems(); 
    for(RssItem rssItem : rssItems) { 
     Log.i("RSS Reader", rssItem.getTitle()); 
     //Log.i(rssItem.getTitle(), rssItem.getDescription()); 
     //Log.i("RSS Content", rssItem.getLink()); 
    } 
}} 

代碼我試圖做的是一次顯示一個項目隨機。我可以把它們全部拉出來,但一次看起來似乎不能得到。任何幫助表示讚賞。謝謝

回答

0

據我所知,你不能用SAX解析器進行隨機訪問。你可以使用DOM解析器而不是這將使你隨機存取能力,或者你可以簡單地讓所有的項目,然後只處理隨機一個選擇,那就是:

ArrayList<RssItem> rssItems = feed.getRssItems(); 

Random rand = new Random(System.currentTimeMillis()); 

//Pick one at random 
Log.i("RSS Reader", rssItems.get(rand.nextInt(rssItems.size())).getTitle(); 
+0

謝謝,正是我需要的! – UndercoverGeek 2012-04-27 18:39:13

0

您可以在的RSSItems存儲在地圖鍵從1開始到numberOfRssItems。然後使用隨機數生成器生成1到numberOfRssItems之間的數字,並使用隨機數作爲關鍵字從地圖中獲取該記錄。