2012-04-27 95 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之间的数字,并使用随机数作为关键字从地图中获取该记录。