如何在此代码中将图像添加为RSS源的一部分

时间:2014-06-02 15:11:55

标签: android

我在RSS Feed中添加了标题,标题链接和说明。但无法将图像添加为RSS源的一部分。

请指导我如何在标记中包含图像并将其显示在RSS源

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listMainView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</ListView>

mainActivity.java

public class ITCutiesReaderAppActivity extends Activity {
/** 
 * This method creates main application view
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set view
    setContentView(R.layout.main);

    try {
        // Create RSS reader
        RssReader rssReader = new RssReader("http://www.itcuties.com/feed/");
        // Get a ListView from main view
        ListView itcItems = (ListView) findViewById(R.id.listMainView);

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

        // Set list view item click listener
        itcItems.setOnItemClickListener(new ListListener(rssReader.getItems(), this));

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

}

rss item

public class RssItem {

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

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;
}

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

listlistener.java

public class ListListener implements OnItemClickListener {

// List item's reference
List<RssItem> listItems;
// Calling activity reference
Activity activity;

public ListListener(List<RssItem> aListItems, Activity anActivity) {
    listItems = aListItems;
    activity  = anActivity;
}

/**
 * Start a browser with url from the rss item.
 */
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(listItems.get(pos).getLink()));

    activity.startActivity(i);

}

}

rss parser.java

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;

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;
    }
}

@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;
    }
}

@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;
        }
    }
}

}

rss reader.java

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();

}

}

0 个答案:

没有答案