1
嗨新的安卓android現在正在開發一個應用程序來列出一些日期從RSS FEED 解析和所有其他工作很好的問題是,我想添加圖像到列表視圖項目。 內容取自RSS FEED。但Rss Feed不包含任何圖像。我需要將圖像添加到列表視圖。並且圖像對於所有列表項都是相同的。如何添加圖像到列表視圖(Rss Feed)
解析使用DefaultHandler完成...
這是我的列表項頁
public class MainActivity extends Activity {
// A reference to the local object
private MainActivity local;
/**
* This method creates main application view
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set view
setContentView(R.layout.activity_main);
// Set reference to this activity
local = this;
GetRSSDataTask task = new GetRSSDataTask();
// Start download RSS task
task.execute("http://manna.christianchannel.us/feed/");
// Debug the thread name
Log.d("Reader", 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("Reader", 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("Reader", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(List<RssItem> result) {
// Get a ListView from main view
ListView itcItems = (ListView) findViewById(R.id.listMainView);
// 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 ListListeners(result, local));
}
}
}
這是我RssParseHandler頁
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;
}
}
}
}
這是我的RSS網頁
public class RssItem {
// item title
private String title;
// item link
private String link;
private String description;
private String icons;
public String getIcons() {
return icons;
}
public void setIcons(String icons) {
this.icons = icons;
}
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;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
如果「所有列表項的圖像相同」,您可以簡單地將其添加到xml佈局中,該佈局描述了每行的內容。然後你可以在你的res/drawable文件夾中添加一個drawable並使用它。 – Michael