嗨,下面是我解析 RSS 并将其填充到 ListView 中的工作代码,但我想添加 CustomAdapter 而不是简单的适配器,这样我就可以在 ListView 中添加一个 ImageView 并设计我自己的 ListView。有人可以帮助编写代码。
SocialFeeds.Java
public class SocialFeeds extends Activity {
// A reference to the local object
private SocialFeeds local;
/**
* This method creates main application view
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set view
setContentView(R.layout.tweet);
// Set reference to this activity
local = this;
GetRSSDataTask task = new GetRSSDataTask();
// Start download RSS task
task.execute("https:url");
// Debug the thread name
Log.d("ITCRssReader", Thread.currentThread().getName());
}
private class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem>> {
private final ProgressDialog dialog = new ProgressDialog(SocialFeeds.this);
@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 onPreExecute() {
dialog.setMessage("Please Wait.");
dialog.setCancelable(true);
dialog.show();
}
@Override
protected void onPostExecute(List<RssItem> result) {
if (dialog.isShowing())
{
dialog.dismiss();
}
// Get a ListView from main view
ListView itcItems = (ListView) findViewById(R.id.list);
// 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));
}
}
}
监听器.java
public class ListListener extends Activity implements OnItemClickListener {
// List item's reference
List<RssItem> listItems;
// Calling activity reference
Activity activity;
AlertDialog.Builder alert;
WebView wv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
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);*/
}
}
RssParseHandler.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;
// A buffer used to build current title being parsed
private StringBuffer currentTitleSb;
// Parsing pubDate indicator
private boolean parsingpubDate;
// A buffer used to build current pubDate being parsed
private StringBuffer currentpubDateSb;
// Parsing description indicator
private boolean parsingDescription;
// A buffer used to build current description being parsed
private StringBuffer currentDescriptionSb;
// 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(0, qName, qName, qName);
} else if ("title".equals(qName)) {
parsingTitle = true;
currentTitleSb = new StringBuffer();
}
else if ("pubDate".equals(qName)) {
parsingpubDate = true;
currentpubDateSb = new StringBuffer();
}
else if ("description".equals(qName)) {
parsingDescription = true;
currentDescriptionSb = new StringBuffer();
}
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;
// Set item's title when we parse item->title tag not the channel title tag
if (currentItem != null) {
// Set item's title here
currentItem.setTitle(currentTitleSb.toString());
}
}
else if ("pubDate".equals(qName)) {
parsingpubDate = false;
// Set item's pubDate when we parse item->pubDate tag not the channel pubDate tag
if (currentItem != null) {
// Set item's pubDate here
currentItem.setpubDate(currentpubDateSb.toString());
}
}
else if ("description".equals(qName)) {
parsingDescription = false;
// Set item's description when we parse item->description tag not the channel description tag
if (currentItem != null) {
// Set item's description here
currentItem.setDescription(currentDescriptionSb.toString());
}
}
else if ("link".equals(qName)) {
parsingLink = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (parsingTitle) {
if (currentItem != null) {
// Here we append the title to the buffer due to network issues.
// Sometimes this characters method is called multiple times for a tag contents.
currentTitleSb.append(new String(ch, start, length));
}
}
if (parsingpubDate) {
if (currentItem != null) {
// Here we append the pubDate to the buffer due to network issues.
// Sometimes this characters method is called multiple times for a tag contents.
currentpubDateSb.append(new String(ch, start, length));
}
}
if (parsingDescription) {
if (currentItem != null) {
// Here we append the description to the buffer due to network issues.
// Sometimes this characters method is called multiple times for a tag contents.
currentDescriptionSb.append(new String(ch, start, length));
}
}
else if (parsingLink) {
if (currentItem != null) {
currentItem.setLink(new String(ch, start, length));
parsingLink = false;
}
}
}
}
RssReader.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;
// A buffer used to build current title being parsed
private StringBuffer currentTitleSb;
// Parsing pubDate indicator
private boolean parsingpubDate;
// A buffer used to build current pubDate being parsed
private StringBuffer currentpubDateSb;
// Parsing description indicator
private boolean parsingDescription;
// A buffer used to build current description being parsed
private StringBuffer currentDescriptionSb;
// 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(0, qName, qName, qName);
} else if ("title".equals(qName)) {
parsingTitle = true;
currentTitleSb = new StringBuffer();
}
else if ("pubDate".equals(qName)) {
parsingpubDate = true;
currentpubDateSb = new StringBuffer();
}
else if ("description".equals(qName)) {
parsingDescription = true;
currentDescriptionSb = new StringBuffer();
}
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;
// Set item's title when we parse item->title tag not the channel title tag
if (currentItem != null) {
// Set item's title here
currentItem.setTitle(currentTitleSb.toString());
}
}
else if ("pubDate".equals(qName)) {
parsingpubDate = false;
// Set item's pubDate when we parse item->pubDate tag not the channel pubDate tag
if (currentItem != null) {
// Set item's pubDate here
currentItem.setpubDate(currentpubDateSb.toString());
}
}
else if ("description".equals(qName)) {
parsingDescription = false;
// Set item's description when we parse item->description tag not the channel description tag
if (currentItem != null) {
// Set item's description here
currentItem.setDescription(currentDescriptionSb.toString());
}
}
else if ("link".equals(qName)) {
parsingLink = false;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (parsingTitle) {
if (currentItem != null) {
// Here we append the title to the buffer due to network issues.
// Sometimes this characters method is called multiple times for a tag contents.
currentTitleSb.append(new String(ch, start, length));
}
}
if (parsingpubDate) {
if (currentItem != null) {
// Here we append the pubDate to the buffer due to network issues.
// Sometimes this characters method is called multiple times for a tag contents.
currentpubDateSb.append(new String(ch, start, length));
}
}
if (parsingDescription) {
if (currentItem != null) {
// Here we append the description to the buffer due to network issues.
// Sometimes this characters method is called multiple times for a tag contents.
currentDescriptionSb.append(new String(ch, start, length));
}
}
else if (parsingLink) {
if (currentItem != null) {
currentItem.setLink(new String(ch, start, length));
parsingLink = false;
}
}
}
}
RssItem.java
public class RssItem {
public int imageId;
// item title
public String title;
// item link
public String link;
// item pubDate
public String pubDate;
// item description
public String description;
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
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 getpubDate() {
return pubDate;
}
public void setpubDate(String pubDate) {
this.pubDate = pubDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public RssItem(int imageId, String title, String description, String link) {
this.imageId = imageId;
this.title = title;
this.description = description;
this.link = link;
}
@Override
public String toString() {
return title + "\n" + description + "\n" + pubDate;
}
}