我使用的是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 :(得分: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();
答案 1 :(得分:0)
您可以将RssItems存储在一个地图中,其中的键从1到numberOfRssItems。然后使用随机数生成器生成1到numberOfRssItems之间的数字,并使用随机数作为关键字从地图中获取该记录。