Android中的Rss Item Parse

时间:2015-12-30 21:25:45

标签: android xml listview rss

我正在制作基本新闻应用。我找到了用于获取rss数据的rss库。我的rss xml有标题,链接和描述。我在ListView上显示了标题。当我点击listview项目时,我想在textview上显示描述。但我不能。我没有解析rss数据。 我的代码在

之下

Rss Item.class

public class RssItem {

// item title
private String title;
// item link
private String link;

private String description;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getLink() {
    return link;
}

public void setLink(String link) {
    this.link = link;
}

public String getDescription(){
    return description;
}
public void setDescription(String description){
    this.description=description;
}

@Override
public String toString() {
    return title;
}

RssParseHandler.class

public class RssParseHandler extends DefaultHandler {

private List<RssItem> rssItems;

// Used to reference item while parsing
private RssItem currentItem;

// Parsing title indicator
private boolean parsingTitle;
// Parsing link indicator
private boolean parsingLink;
private boolean parsingDescription;

public RssParseHandler() {
    rssItems = new ArrayList<RssItem>();
}

public List<RssItem> getItems() {
    return rssItems;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    if ("item".equals(qName)) {
        currentItem = new RssItem();
    } else if ("title".equals(qName)) {
        parsingTitle = true;
    } else if ("link".equals(qName)) {
        parsingLink = true;
    } else if ("description".equals(qName)) {
        parsingDescription = true;
    }
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    if ("item".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;
    } else if ("title".equals(qName)) {
        parsingTitle = false;
    } else if ("link".equals(qName)) {
        parsingLink = false;
    }
    else if ("description".equals(qName)) {
        parsingDescription = false;
    }
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
    if (parsingTitle) {
        if (currentItem != null)
            currentItem.setTitle(new String(ch, start, length));
    } else if (parsingLink) {
        if (currentItem != null) {
            currentItem.setLink(new String(ch, start, length));
            parsingLink = false;
        }
    }
    else if (parsingDescription) {
        if (currentItem != null) {
            currentItem.setDescription(new String(ch, start, length));
            parsingDescription = false;
        }
    }
}

RssReader.class

public class RssReader {

private String rssUrl;

/**
 * Constructor
 * 
 * @param rssUrl
 */
public RssReader(String rssUrl) {
    this.rssUrl = rssUrl;
}

/**
 * Get RSS items.
 * 
 * @return
 */
public List<RssItem> getItems() throws Exception {

    // SAX parse RSS data
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    RssParseHandler handler = new RssParseHandler();

    saxParser.parse(rssUrl, handler);

    return handler.getItems();

}

我在listview上显示标题的活动

public class ThirdActivity extends Activity {

private ThirdActivity local;
private Intent intent;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set view
    setContentView(R.layout.activity_third);

    String newString;
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null) {
            newString= null;
        } else {
            newString= extras.getString("STRING_I_NEED");
        }
    } else {
        newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
    }



    local = this;

    GetRSSDataTask task = new GetRSSDataTask();

    task.execute(newString);

    // Debug the thread name
    Log.d("ITCRssReader", Thread.currentThread().getName());
}

private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem> > {
    @Override
    protected List<RssItem> doInBackground(String... urls) {

        // Debug the task thread name
        Log.d("ITCRssReader", Thread.currentThread().getName());

        try {
            // Create RSS reader
            RssReader rssReader = new RssReader(urls[0]);

            // Parse RSS, get items
            return rssReader.getItems();

        } catch (Exception e) {
            Log.e("ITCRssReader", e.getMessage());
        }

        return null;
    }

    @Override
    protected void onPostExecute(List<RssItem> result) {

        // Get a ListView from main view
        final ListView itcItems = (ListView) findViewById(R.id.listView);

        // Create a list adapter
        ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local,android.R.layout.simple_list_item_1, result);
        // Set list adapter for the ListView
        itcItems.setAdapter(adapter);


        // Set list view item click listener
        itcItems.setOnItemClickListener(new ListListener(result, local));

        itcItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                intent = new Intent(ThirdActivity.this, FourthActivity.class);
                startActivity(intent);






            }
        });
    }


}

0 个答案:

没有答案