Note that there are some explanatory texts on larger screens.

plurals
  1. POextract the images from rss feed
    text
    copied!<p>I'm new to android development application and i suppose to parse rss url into a listview. the code is working but we want to extract the images from the rss feed and display it into the listview.</p> <p>RssItem.java</p> <pre><code>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; } } </code></pre> <p>ListListener.java</p> <pre><code>public class ListListener implements OnItemClickListener { // List item's reference List&lt;RssItem&gt; listItems; // Calling activity reference Activity activity; public ListListener(List&lt;RssItem&gt; aListItems, Activity anActivity) { listItems = aListItems; activity = anActivity; } /** * Start a browser with url from the rss item. */ public void onItemClick(AdapterView&lt;?&gt; 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); } } </code></pre> <p>RssParseHandler.java</p> <pre><code> public class RssParseHandler extends DefaultHandler { private List&lt;RssItem&gt; 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&lt;RssItem&gt;(); } public List&lt;RssItem&gt; 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; } } } } </code></pre> <p>RssReader.java</p> <pre><code> public class RssReader { private String rssUrl; /** * Constructor * * @param rssUrl */ public RssReader(String rssUrl) { this.rssUrl = rssUrl; } /** * Get RSS items. * * @return */ public List&lt;RssItem&gt; 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(); } } </code></pre> <p>ITCutiesReaderAppActivity.java</p> <pre><code>public class ITCutiesReaderAppActivity extends Activity { // A reference to the local object private ITCutiesReaderAppActivity local; /** * This method creates main application view */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set view setContentView(R.layout.main); // Set reference to this activity local = this; GetRSSDataTask task = new GetRSSDataTask(); // Start download RSS task task.execute("http://www.itcuties.com/feed/#sthash.YI6YrEet.dpuf"); // Debug the thread name Log.d("ITCRssReader", Thread.currentThread().getName()); } public class GetRSSDataTask extends AsyncTask&lt;String, Void, List&lt;RssItem&gt; &gt; { @Override protected List&lt;RssItem&gt; 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&lt;RssItem&gt; result) { // Get a ListView from main view ListView itcItems = (ListView) findViewById(R.id.listMainView1); // Create a list adapter ArrayAdapter&lt;RssItem&gt; adapter = new ArrayAdapter&lt;RssItem&gt; (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)); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload