I want an RSS
Feed displayed in an Android app. Basically, it's reading from an XML
file of course. There are two items I want displayed in the Android listView
: title
, description
.
I want the feed to do this:
title
description
title
description
title
description
title
description
instead it is doing this:
title
title
title
title
title
title
description
description
description
description
description
description
I can see why based on how this code is, but I can't figure out how to layout like example number 1.
Here is code:
private class RSSHandler extends DefaultHandler {
public void startElement(String uri, String localName, String qName,
Attributes attrs) throws SAXException {
if (localName.equals("item")) {
item = true;
}
if (localName.equalsIgnoreCase("title")) {
fTitle = true;
}
if (localName.equalsIgnoreCase("description")) {
fDesc = true;
}
}
public void endElement(String namespaceURI, String localName,
String qName) throws SAXException {
}
public void characters(char[] ch, int start, int length)
throws SAXException {
if (fTitle) {
titleResult = titleResult + (new String(ch, start, length))
+ "\t\n\n";
fTitle = false;
}
if (fDesc) {
rssResult = rssResult + (new String(ch, start, length))
+ "\t\n\n";
fDesc = false;
}
}
}
You are maintaining two strings and combining them later, you should combine them as you build them. According to your example you should use something like this:
public void characters(char[] ch, int start, int length)
throws SAXException {
if (fTitle) {
titleResult = titleResult + (new String(ch, start, length))
+ "\t\n";
fTitle = false;
}
if (fDesc) {
titleResult = titleResult + (new String(ch, start, length))
+ "\t\n\n";
fDesc = false;
}
}
Instead you do it:
if (fTitle) {
titleResult = titleResult + (new String(ch, start, length))
+ "\t\n\n";
fTitle = false;
}
if (fDesc) {
rssResult = rssResult + (new String(ch, start, length))
+ "\t\n\n";
fDesc = false;
}
Look you are composing two strings one with a lot of titles and othr with a lot of descriptions.
Try to make a List<String>
and add yours objects there:
List<String> feeds = new ArrayList<String>();
...
public void characters(char[] ch, int start, int length)
throws SAXException {
String feed = new String("");
if (fTitle) {
feed = new String(ch, start, length)
+ "\t\n\n";
fTitle = false;
}
if (fDesc) {
feed = feed + (new String(ch, start, length))
+ "\t\n\n";
fDesc = false;
}
feeds.add(feed);
}
Then your can use a print to show your list:
for(String feed : feeds) {
<Use your string>
feed.doSometing();
}
But the best pratice are you make a Feed Object to store the title and the description, like:
List<Feed> feeds = new ArrayList<Feed>();
Feed feed = new Feed(title, description);
feeds.add(feed);
for(Feed feed : feeds) {
feed.getTitle().doSometing();
feed.getDescription().doSometing();
}
=)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.