所以我对这个ArrayAdapter 2个问题:
1.在构造函数中的第三个参数List<RSSItem> list
自动分析了getView
,并position
将通过在本list
每个项目重复? (所以唯一重要的事情就是打电话super
通过这个列表作为参数?)arrayAdapter =无限循环? (参见:代码)
2.在代码的末尾,我们有new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());
这是如何工作的,而不是在代码中创建一个infite循环?因为在arrayAdapter的末尾,类会自行调用以重新启动适配器......适配器如何结束?
这里的代码(来源:http://android-er.blogspot.com/2010/07/simple-rss-reader-with-options-menu-to.html):
public class AndroidRssReader extends ListActivity {
private RSSFeed myRssFeed = null;
TextView feedTitle;
TextView feedDescribtion;
TextView feedPubdate;
TextView feedLink;
public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
public MyCustomAdapter(Context context, int textViewResourceId,
List<RSSItem> list) {
super(context, textViewResourceId, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
View row = convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.row, parent, false);
}
TextView listTitle=(TextView)row.findViewById(R.id.listtitle);
listTitle.setText(myRssFeed.getList().get(position).getTitle());
TextView listPubdate=(TextView)row.findViewById(R.id.listpubdate);
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
if (position%2 == 0){
listTitle.setBackgroundColor(0xff101010);
listPubdate.setBackgroundColor(0xff101010);
}
else{
listTitle.setBackgroundColor(0xff080808);
listPubdate.setBackgroundColor(0xff080808);
}
return row;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
feedTitle = (TextView)findViewById(R.id.feedtitle);
feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
feedPubdate = (TextView)findViewById(R.id.feedpubdate);
feedLink = (TextView)findViewById(R.id.feedlink);
readRss();
}
private void readRss(){
feedTitle.setText("--- wait ---");
feedDescribtion.setText("");
feedPubdate.setText("");
feedLink.setText("");
setListAdapter(null);
Toast.makeText(this, "Reading RSS, Please wait.", Toast.LENGTH_LONG).show();
try {
URL rssUrl = new URL("http://www.gov.hk/en/about/rss/govhkrss.data.xml");
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (myRssFeed!=null)
{
Calendar c = Calendar.getInstance();
String strCurrentTiime = "\n(Time of Reading - "
+ c.get(Calendar.HOUR_OF_DAY)
+ " : "
+ c.get(Calendar.MINUTE) + ")\n";
feedTitle.setText(myRssFeed.getTitle() + strCurrentTiime);
feedDescribtion.setText(myRssFeed.getDescription());
feedPubdate.setText(myRssFeed.getPubdate());
feedLink.setText(myRssFeed.getLink());
MyCustomAdapter adapter =
new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());
setListAdapter(adapter);
}
}
编辑: 在这个例子中,如何 “视图” 可以为空?
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, List<String> objects) {
super(context, R.layout.list_item, R.id.text, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Wrapper wrapper;
if (view == null) {
view = mInflater.inflate(R.layout.list_item, null);
wrapper = new Wrapper(view);
view.setTag(wrapper);
} else {
wrapper = (Wrapper) view.getTag();
}
感谢
感谢Adil,好吧现在对我更加清楚,非常感谢 – Paul 2012-01-04 08:58:49
Adil,我可以问你一些其他问题:在我的编辑中,如何查看可以为空?我们通过'super'调用传递了'R.layout.list_item',那么视图如何可以是“空”?谢谢 – Paul 2012-01-04 09:57:06
+1很好的解释。 – 2012-01-04 09:57:23