0

彼は sax パーサーを使用しており、すべてをリストする代わりに、一度に 1 つのレコードを取り出したいと考えています。ここに私が引っ張っている私の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());
    }
}}

私がやろうとしているのは、一度に 1 つのアイテムをランダムに表示することです。全部引くことはできますが、一度に 1 つずつ取得することはできないようです。どんな助けでも大歓迎です。ありがとう

4

2 に答える 2

0

RssItemsは、1からnumberOfRssItemsまでのキーを持つマップに格納できます。次に、乱数ジェネレーターを使用して1からnumberOfRssItemsまでの数値を生成し、乱数をキーとして使用してマップからそのレコードを取得します。

于 2012-04-27T15:24:46.940 に答える
0

私の知る限り、SAX パーサーでランダム アクセスを行うことはできません。代わりにDOMパーサーを使用してランダムアクセス機能を提供するか、単にすべてのアイテムを取得してから、ランダムに選択された1つだけを処理することができます。

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();
于 2012-04-27T14:54:48.807 に答える